问题
In my web.config
file I have custom errors enabled:
<customErrors mode="On" defaultRedirect="~/Error">
<error redirect="~/Error/NotFound" statusCode="404" />
</customErrors>
My NotFound
action:
public ActionResult NotFound()
{
Response.StatusCode = 404; //no issues when this is not set
return View();
}
The problem:
This configuration works fine on a local server, but when I move it to a remote server custom 404 pages are not shown (IIS default 404 is displayed) unless the status code of NotFound
action is set to 200.
Could someone explain what's going on?
回答1:
You also want to disable IIS custom errors by setting TrySkipIisCustomErrors to true.
public ActionResult NotFound()
{
Response.StatusCode = 404;
Response.TrySkipIisCustomErrors = true; <---
return View();
}
来源:https://stackoverflow.com/questions/29522714/custom-404-page-not-showing-when-statuscode-is-not-200