【问题标题】:How to redirect all httpErrors to custom url?如何将所有 httpErrors 重定向到自定义 url?
【发布时间】:2013-09-07 09:44:03
【问题描述】:

这些是 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....等..等...

我们是否可以将所有错误重定向到同一个 url 而无需输入所有错误代码?

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

【问题讨论】:

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


    【解决方案1】:

    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>
    

    【讨论】:

    • 我在 IIS Express 配置文件中进行了更改,但无济于事。解锁我计算机上安装的 IIS 也是如此。是否需要更改其他内容才能解决锁定问题?
    • 现代 IIS Express 将配置存储在 $(ProjectRoot)\.vs\config 目录中。你编辑了吗?
    • 谢谢。我没有意识到它已经移动了。在新位置进行编辑就可以了。
    【解决方案2】:

    试试这个,

    添加到 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

    【讨论】:

    • OP 询问是否有办法将所有错误重定向到错误页面,而无需在 web.config 中输入所有可能的 HTTP 错误。你只是重复了他的问题。
    • @webworm 我选择了这个作为回答,因为我认为他间接回答了这个问题。答案是“否”,必须单独定义每个 http 错误代码。没有像错误代码 = 之类的东西 :)
    • Oops.aspx的内容(源代码)是什么?
    • 您可以根据需要添加自己的页面。
    猜你喜欢
    • 1970-01-01
    • 2012-01-24
    • 2018-01-10
    • 2015-03-11
    • 2013-10-27
    • 2012-03-12
    • 2015-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多