Custom 404 Page not showing when StatusCode is not 200

微笑、不失礼 提交于 2019-12-13 15:11:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!