ASP.NET MVC自定义错误页面(StatusCode 404抛出500)

时间:2022-10-10 16:54:10

I've got customErrors set in my web.config

我在web.config中设置了customErrors

<customErrors mode="On" defaultRedirect="/Error/GeneralError">
    <error statusCode="404" redirect="/Error/NotFound"/>
</customErrors>

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:

这在当地工作正常。 404会抛出404.在共享主机上它会抛出标准服务器404页面,除非我专门设置404指向/ Error / NotFound。没关系。现在它将显示自定义404页面,但响应状态代码为200.所以如果我尝试抛出Response.StatusCode = 404;在我在ErrorController中的NotFound操作中,如下所示:

public class ErrorController : Controller
{
    public ActionResult NotFound()
    {
        Response.StatusCode = 404;
        return View();
    }
}

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.

服务器抛出状态代码500内部服务器错误,但我的GeneralError页面没有显示,只是一个没有源的空白页面。

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.

我尝试了很多不同的组合,但我似乎无法找到如何让它显示我的自定义404页面以及404响应。

Any ideas?

有任何想法吗?

2 个解决方案

#1


20  

I found out some interesting information here: http://blog.angrypets.com/2008/03/responsetryskip.html

我在这里找到了一些有趣的信息:http://blog.angrypets.com/2008/03/responsetryskip.html

Response.TrySkipIisCustomErrors = true; 

Setting TrySkipIisCustomErrors to true after the Response.StatusCode = 404; takes care of the issue.

在Response.StatusCode = 404之后将TrySkipIisCustomErrors设置为true;处理这个问题。

#2


1  

Don't use a customErrors element (except to turn mode="On" for local testing). Instead, add an Application_EndRequest method to your MvcApplication per Marco's Better-Than-Unicorns MVC 404 Answer.

不要使用customErrors元素(除了将mode =“On”用于本地测试)。相反,根据Marco的Better-Than-Unicorns MVC 404 Answer,为您的MvcApplication添加Application_EndRequest方法。

#1


20  

I found out some interesting information here: http://blog.angrypets.com/2008/03/responsetryskip.html

我在这里找到了一些有趣的信息:http://blog.angrypets.com/2008/03/responsetryskip.html

Response.TrySkipIisCustomErrors = true; 

Setting TrySkipIisCustomErrors to true after the Response.StatusCode = 404; takes care of the issue.

在Response.StatusCode = 404之后将TrySkipIisCustomErrors设置为true;处理这个问题。

#2


1  

Don't use a customErrors element (except to turn mode="On" for local testing). Instead, add an Application_EndRequest method to your MvcApplication per Marco's Better-Than-Unicorns MVC 404 Answer.

不要使用customErrors元素(除了将mode =“On”用于本地测试)。相反,根据Marco的Better-Than-Unicorns MVC 404 Answer,为您的MvcApplication添加Application_EndRequest方法。