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

mjb*_*mjb 7 asp.net web-config c#-4.0

这些是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>
Run Code Online (Sandbox Code Playgroud)

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

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

我们可以设置它将所有错误重定向到相同的URL而不键入所有错误代码吗?

<error statusCode="400" .....
<error statusCode="401" .....
<error statusCode="403" .....
<error statusCode="404" .....
<error statusCode="xxx" ....
Run Code Online (Sandbox Code Playgroud)

Man*_*rma 6

试试这个,

添加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>
Run Code Online (Sandbox Code Playgroud)

<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>
Run Code Online (Sandbox Code Playgroud)

更多关于这个http://www.iis.net/configreference/system.webserver/httperrors

  • OP询问是否有办法重定向到错误页面以查找所有错误,而无需在web.config中输入每个可能的HTTP错误.你刚刚重复了他的问题. (3认同)

Der*_*ter 6

httpErrors部分具有defaultPath属性.

<system.webServer>
  <httpErrors defaultPath="Error.html" defaultResponseMode="File">
    <clear />
  </httpErrors>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

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

但是,我不使用它,因为defaultPath默认情况下锁定在IIS Express中.需要编辑%homepath%\Documents\IISExpress\config\applicationHost.config才能解锁.

<httpErrors lockAttributes="allowAbsolutePathsWhenDelegated,defaultPath">
  <!-- ... -->
</httpErrors>
Run Code Online (Sandbox Code Playgroud)

  • 现代IIS Express将配置存储在$(ProjectRoot)\.vs\config目录中.你编辑了吗? (3认同)