mvc 5 error handling not working after third folde

2019-07-10 09:15发布

I have an MVC 5 app and with custom errors defined in web.config:

<customErrors mode="RemoteOnly" redirectMode="ResponseRedirect" defaultRedirect="~/Errors/GeneralError">
  <error statusCode="404" redirect="~/Errors/NotFound" />
  <error statusCode="500" redirect="~/Errors/ServerError" />
</customErrors>

if I type a none existing url it will handle it just fine as long as it is 3 levels/folders maximum. I also have Elmah installed and it will log the error. On the other hand, if I try accessing a none existing url with 4 or more levels/folders, the error will not be handled. It won't redirect to the custom error page, or the default error page. Instead, it will return a blank page with a 404 header. meaning:

  • website.com/blahblah
  • website.com/a/blahblah
  • website.com/a/b/blahblah

will be handled, but website.com/a/b/c/blahblah and above won't. Additionally, I have some pages with 4 or more folders, and they work fine. This problem occurs on local production computer and on the published site on Azure. Anyone has an insight of why?

Solution

Thanks to @Nicholas Patton's link I made these chages:


(1) I removed customErrors completely


(2) added this to web.config:

<system.webServer>
  <httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404"/>
  <error statusCode="404" path="/Errors/NotFound" responseMode="ExecuteURL" />
  <remove statusCode="403"/>
  <error statusCode="403" path="/Errors/NotFound" responseMode="ExecuteURL" />
  <remove statusCode="500"/>
  <error statusCode="500" path="/Errors/ServerError" responseMode="ExecuteURL" />
</httpErrors>

(3) In contrary to the article's suggestion using .aspx files I used regular actions in MVC. For example, in controller Error I used NotFound action like so:

[Route("Errors/NotFound")]
public ActionResult NotFound()
{
    Response.StatusCode = 404;
    return View("Error404");
}

There are a few differences now. the 404 page will not be redirect to Error/NotFound?aspxerrorpath=/errorURL/a/b/c it will simply show the custom error page in the original link without redirection. If you still want the aspxerrorpath value you can simply get the URL in the action like this:

[Route("Errors/NotFound")]
public ActionResult NotFound()
{
    string aspxerrorpath = Request.RawUrl;
    // do whatever with aspxerrorpath 

    Response.StatusCode = 404;
    return View("Error404");
}

Nonetheless, Elmah still doesn't log 404 errors above 3 folders. But this is a different issue for a new question.

1条回答
祖国的老花朵
2楼-- · 2019-07-10 09:46

Read this: https://dusted.codes/demystifying-aspnet-mvc-5-error-pages-and-error-logging

From the link above:

...what if someone navigates to a URL which isn't handled by ASP.NET? For example try navigating to http://{your-website}/a/b/c/d/e/f/g. The route is not mapped to ASP.NET and therefore the Application_Error event will not be raised.

This is a really great tutorial if you really want your custom error pages to be beefy. :)

查看更多
登录 后发表回答