Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to handle Error 404 in ASP.NET MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 441
    Comment on it

    In the following article we will learn how to handle 404 errors in ASP.NET MVC gracefully.

    The first step will be to set up the custom errors page by making the following settings in web.config:

    <system.web>
            <compilation debug="false" targetframework="4.0">
            <customerrors mode="On"> <br>
            </customerrors>
     </compilation></system.web>
    

    Next we make changes to ensure we see the stack trace in case of error. We also use the Request.IsLocal property that tells us if the Request is coming from the Local machine or a Remote machine so that end users do not see the yellow screen of death. The end users will only see a generic error message.Below is the code to achieve the desired result.

    @model System.Web.Mvc.HandleErrorInfo 
    @{
       ViewBag.Title="Error";
    }
    <h1 class="error">Error.
    @{
      if (Request.IsLocal)
      {
        if (@Model != null &amp;&amp; @Model.Exception != null)
        {
            </h1><h4> Exception Details: @Model.Exception.Message</h4>
            <h5> Stack Trace: @Model.Exception.StackTrace  </h5>       
        }  
        else
        {
          <h4>Exception is Null</h4>
        }
      }
      else
      {   
        <h4>An unexpected error occurred on the website and has been reported to the administrator.</h4>  
      }
    }
    

    We have set up a basic error handling mechanism for our ASP.NET application . Now we will focus on handling 404 errors. We will update our web.config to route 404 exceptions to a new view that we will create.

    <customerrors mode="RemoteOnly" defaultredirect="Error">
    <error code="404" path="~/Views/Shared/PageNotFound.cshtml">
    </error></customerrors>
    

    We now create and Action method in Eror controller

    public class ErrorController
    { 
        public ActionResult PageNotFound
        {
            Response.StatusCode=404;
            Response.TrySkipisCustomErrors=true;
            return View();
         }
    }
    

    We are using Response.TrySkipisCustomErrors=true; to stop the IIS from serving it's own 404 page.

    Lastly we create the routes in global.asax

    routes.MapRoute(
            "Error404",
            "NotFound",
            new { controller = "Error", action = "PageNotFound" }
            );

    Using the process shown in the above article, we can handle HTTP 404 errors in a proper manner.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: