Monday, 24 December 2018

How to handle HTTP 404 errors

The resource cannot be found. HTTP 404.

To display a friendly error page:

Step 1: Add "ErrorController" to controllers folder. Copy and paste the following code.

public class ErrorController Controller
{
    public ActionResult NotFound()
    {
        return View();
    }
}

Step 2: Right-click on "Shared" folder and add "NotFound.cshtml" view. 

<h2>Please check the URL. The page you are looking for cannot be found</h2>

Step 3: Change "customErrors" element in web.config as shown below.

<customErrors mode="On">
  <error statusCode="404" redirect="~/Error/NotFound"/>
</customErrors> 

No comments:

Post a Comment