如何将所有httpErrors重定向到自定义URL?

时间:2013-09-07 09:44:04

标签: asp.net c#-4.0 web-config

这些是web.config中的代码:

<system.web>
  <customErrors mode="Off" >
  </customErrors>
</system.web>
<system.webServer>
  <httpErrors errorMode="Custom" existingResponse="Replace">
    <clear />
    <error statusCode="404" prefixLanguageFilePath="" path="/ResourceNotFound" responseMode="ExecuteURL" />
    <error statusCode="500" prefixLanguageFilePath="" path="/ResourceNotFound" responseMode="ExecuteURL" />
    </httpErrors>
</system.webServer>

以上设置仅重定向404和500的httpError。

但不是手动添加400,401,403 ....等所有错误代码......等等......

我们可以设置它将所有错误重定向到同一个网址而不输入所有错误代码吗?

<error statusCode="400" .....
<error statusCode="401" .....
<error statusCode="403" .....
<error statusCode="404" .....
<error statusCode="xxx" ....

2 个答案:

答案 0 :(得分:6)

httpErrors部分有defaultPath属性。

<system.webServer>
  <httpErrors defaultPath="Error.html" defaultResponseMode="File">
    <clear />
  </httpErrors>
</system.webServer>

http://www.iis.net/configreference/system.webserver/httperrors

但是,我没有使用它,因为defaultPath默认情况下在IIS Express中被锁定。需要修改%homepath%\Documents\IISExpress\config\applicationHost.config才能解锁。

<httpErrors lockAttributes="allowAbsolutePathsWhenDelegated,defaultPath">
  <!-- ... -->
</httpErrors>

答案 1 :(得分:5)

试试这个,

添加web.config文件。

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

<httpErrors existingResponse="Replace" defaultResponseMode="ExecuteURL" errorMode="Custom">
    <remove statusCode="404" />
    <error statusCode="404" path="/ErrorPages/Oops.aspx" responseMode="ExecuteURL"/>
    <remove statusCode="401" />
    <error statusCode="401" path="/Account/Login.aspx" responseMode="ExecuteURL"/>
    <remove statusCode="501"/>
    <error statusCode="501" path="/ErrorPages/Oops.aspx" responseMode="ExecuteURL"/>
    <remove statusCode="411"/>
    <error statusCode="411" path="/ErrorPages/Oops.aspx" responseMode="ExecuteURL"/>
    <remove statusCode="403"/>
    <error statusCode="403" path="/ErrorPages/Oops.aspx" responseMode="ExecuteURL"/>
</httpErrors>

以及有关此http://www.iis.net/configreference/system.webserver/httperrors

的更多信息