Asp.net

IIS7 上託管的 ASP.NET 應用程序忽略自定義錯誤並回退到 IIS 錯誤

  • June 26, 2013

我有一個 C# Web 表單 ASP.NET 4.0 Web 應用程序,由於某種原因,我的 web.config 的 system.web 部分中定義的自定義錯誤被完全忽略,它會回退 IIS 錯誤。

這被完全忽略

 <system.web>
   <customErrors mode="On">
     <error statusCode="500" redirect="~/Error" />
     <error statusCode="404" redirect="~/404" />
     <error statusCode="403" redirect="~/Error" />
   </customErrors>
 </system.web>

這部分接管

 <system.webServer>
   <httpErrors>
     &lt;!--&lt;clear /&gt;--&gt;
     &lt;remove statusCode="500" subStatusCode="-1" /&gt;
     &lt;remove statusCode="404" subStatusCode="-1" /&gt;
     &lt;error statusCode="404" subStatusCode="-1" path="/App1/404" responseMode="Redirect" /&gt;
     &lt;error statusCode="500" prefixLanguageFilePath="" path="/App1/Error" responseMode="Redirect" /&gt;
   &lt;/httpErrors&gt;
 &lt;/system.webServer&gt;

這將是一個小小的不便,除了它回退到 IIS 原生而不是我的應用程序這一事實之外,它完全繞過了 Elmah 正確記錄我的 404 異常。

**編輯:**為了避免任何這樣的建議,我只在 customErrors 停止工作後添加了 httpErrors 配置,所以我會有任何東西。

要禁用 IIS 錯誤消息,您必須設置

 Response.TrySkipIisCustomErrors = true;

在您的錯誤頁面中。之後,您的錯誤消息應該沒有問題地顯示。

而不是使用:

Response.TrySkipIisCustomErrors = true;

在 Web.config 中使用它:

&lt;httpErrors errorMode="Custom" existingResponse="Replace"&gt;

引用自:https://stackoverflow.com/questions/3166523