2011-10-11 70 views
3

我在IIS 7上本地運行的Asp.Net 4中有一個Web應用程序。 我需要顯示一個自定義頁面(404)和一個500來代替IIS的默認頁面。 Web.Config中使用此httpErrors配置示例<httpErrors>

<system.webServer> 
    <httpErrors> 

我的網站是在

C:\inetpub\wwwroot\mysite\ 

我的自定義錯誤頁:

C:\inetpub\wwwroot\mysite\ErrorPages\404.htm 
C:\inetpub\wwwroot\mysite\ErrorPages\505.htm 

我不明白它是如何工作的。請給我一個代碼示例嗎?

感謝

回答

11

我解決了我的問題無線這個。

<httpErrors errorMode="Custom"> 
    <remove statusCode="404" subStatusCode='-1' /> 
    <remove statusCode="500" subStatusCode='-1' /> 
    <error statusCode="404" path="/404.aspx" prefixLanguageFilePath="" responseMode="ExecuteURL" /> 
    <error statusCode="500" path="/500.aspx" prefixLanguageFilePath="" responseMode="ExecuteURL" /> 
    </httpErrors> 

這需要在Web.config去,下<configuration>><system.webServer>

例如

<configuration> 
    <system.webServer> 
     <httpErrors ...> 
      // define errors in here ... 
     </httpErrors> 
    </system.webServer> 
</configuration> 
+1

您可以省略'prefixLanguageFilePath =「」'。 – Oliver

+0

感謝分享! +1 – GibboK

+1

建議:包含父元素,以便讀者可以在' Myster

5

下面是一個例子,希望它能幫助提意見

<system.web> 
<customErrors mode="RemoteOnly" defaultRedirect="default.aspx"> 
<error statusCode="404" redirect="~/ErrorPages/404.htm"/> 
<error statusCode="500" redirect="~/ErrorPages/505.htm"/> 
</customErrors> 
</system.web> 

編輯:這是我認爲的例子,你需要

<configuration> 
    <system.webServer> 
     <httpErrors errorMode="DetailedLocalOnly" defaultResponseMode="File" > 
     <remove statusCode="500" /> 
     <error statusCode="500" 
      prefixLanguageFilePath="C:\Contoso\Content\errors" 
      path="500.htm" /> 
     </httpErrors> 
    </system.webServer> 
</configuration> 

http://www.iis.net/ConfigReference/system.webServer/httpErrors/error

+0

感謝,這是工作asp.net,但我的服務器上,我需要即使靜態內容 – GibboK

+0

對不起的配置,我不明白。你能解釋一下嗎? –

+0

爲我的理解您的代碼工作,如果我需要一個不存在的頁面(擴展名爲.aspx)例如:foo.aspx其中foo.aspx在服務器上不存在。我需要改用 ....你有任何例子嗎? – GibboK