2011-11-28 29 views
5

我試圖使用簡單的HTML頁面時503服務不可預料的錯誤來。httperrors在web.config

我使用下面的web.config中的system.webservers

<httpErrors errorMode="Custom"> 
    <remove statuscode="503" substatuscode="-1"> 
    <error statuscode="503" responseMode="File" path="Views/Shared/IISError.htm"> 
</httpErrors> 

這是NT工作。當我停止我的應用程序時,我仍然在獲取IIS默認頁面。

我正在使用mvc3,剃鬚刀應用程序。

+1

嘗試'' –

回答

2

問題是,您使用的是VisualStudio Development Server還是IIS7 Express?

如果您使用卡西尼裝置(VSD),那麼你應該

<customErrors mode="On" > 
    <error statusCode="503" redirect="/Views/Shared/Error.htm"/> 
</customErrors> 

嘗試,因爲httpErrors是隻有IIS7處理的新的結構。你可以找到一些更多的信息:What is the difference between customErrors and httpErrors?http://www.iis.net/ConfigReference/system.webServer/httpErrors

+0

我使用IIS 7.0 –

+0

你是如何讓你的IIS拋出503 ?如果你通過應用程序來完成它,它將通過Asp.Net處理程序。 httpErrors指令適用於不通過Asp.Net的內容 – torm

+0

我在IIS中停止我的應用程序池。這顯示我默認的503頁而不是我的頁面。 –

7

我花了一段時間來弄清楚這一點...但我認爲這可能會幫助您:

首先在IIS 7配置錯誤需要使用下面的部分它做:

<system.webServer> 
    <httpErrors existingResponse="Replace" defaultResponseMode="Redirect" errorMode="Custom"> 
     <remove statusCode="503"/> 
     <error statusCode="503" responseMode="Redirect" path="Views/Shared/IISError.htm"/>   
    </httpErrors> 
    </system.webServer> 

這種配置工作,但您可能會收到指示不能覆蓋的httpErrors部分,如果是那樣的話,請按照下面的步驟的錯誤:

  1. 打開C:\Windows\System32\inetsrv\config\applicationHost.config

  2. 變化:

    <section name="httpErrors" overrideModeDefault="Deny" /> 
    

    要:

    <section name="httpErrors" overrideModeDefault="Allow" /> 
    
1

根據您的意見之一,就好像你停止你的應用程序池出現。

如果您停止應用程序池,則無法在web.config中設置自定義錯誤。您將需要在IIS中執行此操作。

1

在Mvc 5.1.1和IIS 7.5中,需要反斜槓來指示文件響應模式的子文件夾。

C#:

[HttpGet] 
[AllowAnonymous] 
public ActionResult Login() 
{ 
    try 
    { 
     var allowLogin = false; 

     if(allowLogin == false) 
      return new HttpStatusCodeResult(403); 
    } 
} 

Web.config文件:

<system.web> 
    <!--customErrors tag is not required --> 
</system.web> 
<httpErrors errorMode="Custom" existingResponse="Replace" defaultResponseMode="File" > 
    <remove statusCode="400" subStatusCode="-1" /> 
    <remove statusCode="401" subStatusCode="-1" /> 
    <remove statusCode="403" subStatusCode="6" /> 
    <remove statusCode="403" subStatusCode="-1" /> 
    <remove statusCode="503" subStatusCode="-1" /> 
    <remove statusCode="500" subStatusCode="-1" /> 
    <clear/> 
    <error statusCode="400" subStatusCode="-1" path="ErrorPages\400.html" /> 
    <error statusCode="401" subStatusCode="-1" path="ErrorPages\401.html" /> 
    <error statusCode="403" subStatusCode="6" path="ErrorPages\Restrict.html" /> 
    <error statusCode="403" subStatusCode="-1" path="ErrorPages\403.html" /> 
    <error statusCode="503" subStatusCode="-1" path="ErrorPages\503.html" /> 
    <error statusCode="500" subStatusCode="-1" path="ErrorPages\500.html" /> 
</httpErrors>