ASP.NET MVC自定义错误页(状态code 404抛出一个500) [英] ASP.NET MVC Custom Error page (StatusCode 404 throws a 500)
问题描述
我有在的customErrors我的web.config设置
I've got customErrors set in my web.config
<customErrors mode="On" defaultRedirect="/Error/GeneralError">
<error statusCode="404" redirect="/Error/NotFound"/>
</customErrors>
这正常工作本地。 404抛出404在共享托管它抛出了标准服务器的404页面,除非我专门设置404为指向/错误/ NOTFOUND。没关系。现在,它会显示除了响应状态code中的自定义404页200.所以,如果我尝试扔Response.Status code = 404;在ErrorController我NOTFOUND动作是这样的:
This works fine locally. A 404 throws a 404. On the shared hosting it throws up the standard server 404 page unless I specifically set 404 to point to /Error/NotFound. That's fine. Now it will show the custom 404 page except the response status code is 200. So if I try to throw Response.StatusCode = 404; in my NotFound action in ErrorController like this:
public class ErrorController : Controller
{
public ActionResult NotFound()
{
Response.StatusCode = 404;
return View();
}
}
服务器抛出一个状态code 500内部服务器错误,但我的GeneralError页面不显示,只是一个空白页,没有源代码。
the server throws a status code 500 Internal Server Error but my GeneralError page doesn't show, just a blank white page with no source.
我已经尝试了许多不同的组合,但我似乎无法找到如何使它显示我的自定义404页面,404响应一起。
I've tried many different combinations but I can't seem to find how to make it show my custom 404 page along with a 404 response.
任何想法?
推荐答案
我在这里发现了一些有趣的信息:<一href=\"http://blog.angrypets.com/2008/03/responsetryskip.html\">http://blog.angrypets.com/2008/03/responsetryskip.html
I found out some interesting information here: http://blog.angrypets.com/2008/03/responsetryskip.html
Response.TrySkipIisCustomErrors = true;
在Response.Status code = 404后设置TrySkipIisCustomErrors为true;采取这一问题的关心。
Setting TrySkipIisCustomErrors to true after the Response.StatusCode = 404; takes care of the issue.
这篇关于ASP.NET MVC自定义错误页(状态code 404抛出一个500)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!