且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET MVC应用程序的自定义错误页,共享主机环境不显示

更新时间:2023-01-10 10:16:28

我已经找到了解决方案,这是令人难以置信的简单。原来,问题是实际上是在IIS7。虽然调试在Visual Studio这个问题,我看到了,我以前没有注意到的Htt presponse对象的属性:

I've found the solution and it's incredibly simple. Turns out the problem was actually in IIS7. While debugging this issue in Visual Studio I saw a property of the HttpResponse object that I hadn't noticed before:

public bool TrySkipIisCustomErrors { get; set; }

这导致我这竟然一个通过精彩的博客贴子离我最近的搜索引擎里克施特拉尔,另一个是关于 angrypets.com 以及this问题就在这里SO 。这些链接解释血淋淋的细节比我好多了,但是从里克的帖子这句话抓住了它pretty得好:

This lead me to my nearest search engine which turned up a great blog post by Rick Strahl and another on angrypets.com as well as this question here on SO. These links explain the gory details much better than I can, but this quote from Rick's post captures it pretty well:

在这里发生真正的混乱,因为错误是由被困
  ASP.NET,但最终还是由IIS处理,看起来在
  500状态code和返回库存IIS错误页面。

The real confusion here occurs because the error is trapped by ASP.NET, but then ultimately still handled by IIS which looks at the 500 status code and returns the stock IIS error page.

这似乎也是这种行为是特定于IIS7集成模式。从 MSDN

It also seems this behavior is specific to IIS7 in Integrated mode. From msdn:

在经典模式IIS 7.0中运行TrySkipIisCustomErrors
  属性默认值是true。当在集成模式下运行时,
  TrySkipIisCustomErrors属性默认值是假的。

When running in Classic mode in IIS 7.0 the TrySkipIisCustomErrors property default value is true. When running in Integrated mode, the TrySkipIisCustomErrors property default value is false.

所以基本上所有我最后不得不做的就是添加 Response.TrySkipIisCustomErrors = TRUE; 之后的任何code,设置了响应.STATUS code 为500或503,现在一切功能而设计的。

So essentially all I ended up having to do is add Response.TrySkipIisCustomErrors = true; right after any code that sets the Response.StatusCode to 500 or 503 and everything now functions as designed.