ASP.NET MVC 4 - Exception Handling Not Working

前端 未结 4 1472
执念已碎
执念已碎 2020-12-30 09:34

When an error occurs in my ASP.NET MVC 4 application, I would like to customize a view for the user depending on the type of error. For example, page not found or an except

相关标签:
4条回答
  • 2020-12-30 09:56

    I also went on a seamingly endless journey of reading SO answers and assorted blog postings trying to get custom error pages to work. Below is what finally worked for me.

    The first step is to use IIS Express for debugging instead of the built-in Cassini web server to "guarantee" that the debug experience will mirror the live environment.

    Create a controller to handle application errors and add an action for each custom error you will handle. The action should do any logging, set the status code, and return the view.

    public class ErrorsController : Controller
    {
        // 404
        [HttpGet]
        public ActionResult NotFound()
        {
            Response.StatusCode = (int)HttpStatusCode.NotFound;
            return View();
        }
    
        // I also have test actions so that I can verify it's working in production.
        [HttpGet]
        public ActionResult Throw404()
        {
            throw new HttpException((int)HttpStatusCode.NotFound, "demo");
        }
    }
    

    Configure the customErrors section in web.config to redirect to your custom error actions.

      <system.web>
        <customErrors mode="RemoteOnly" defaultRedirect="Errors/InternalServerError">
          <error statusCode="400" redirect="Errors/BadRequest" />
          <error statusCode="403" redirect="Errors/Forbidden" />
          <error statusCode="404" redirect="Errors/NotFound" />
          <error statusCode="500" redirect="Errors/InternalServerError" />
        </customErrors>
    

    Add the httpErrors section to system.webServer and set the errorMode to Detailed in web.config. Why this works is a mystery to me, but this was the key.

      <system.webServer>
        <httpErrors errorMode="Detailed" />
    

    Add a catchall route last to the defined routes to direct 404s to the custom page.

                // catchall for 404s
            routes.MapRoute(
                "Error",
                "{*url}",
                new {controller = "Errors", action = "NotFound"});
    

    You may also want to create a custom HandleErrorAttribute and register it as a global filter to log 500 errors.

    These steps worked for me in both development (IIS Express) and production (IIS7) environments. You need to change customErrors mode="On" to see the effect in development.

    0 讨论(0)
  • 2020-12-30 10:08

    I faced a similar problem and lost sometime trying to work out what was going on. So just in case any others face a similar problem here is what my problem was.

    The error page was trying to use my _Layout page. Just ensure that you Error.cshtml page has

    @{ Layout = null; }

    0 讨论(0)
  • 2020-12-30 10:10

    I seem to recall that you had to call the page from a non-localhost IP address (typically another computer). And it has to be an IIS based server, not the built-in development server (so IIS or IIS Express, but then you have to configure IIS Express for external access, which is a pain).

    You can in fact debug it, you have to configure your local server on your debug computer to accept external requests, then call your local server from the remote server.

    0 讨论(0)
  • 2020-12-30 10:16

    Try adding the following attribute to the customErrors tag:

    defaultRedirect="Error"
    

    This explicitly defines to which view MVC should redirect the user when the error is thrown and no view is specified by default in the attribute. This will of course only work if Error.cshtml exists in the Shared views folder.

    0 讨论(0)
提交回复
热议问题