加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – Web.config – 自定义错误页面无效

发布时间:2020-12-16 07:13:12 所属栏目:asp.Net 来源:网络整理
导读:我已经尝试了以下(并尝试使用已注释掉的未注释)但只得到一个错误: The resource you are looking for has been removed,had its name changed,or is temporarily unavailable. 在已发布的网站项目的web.config中: system.webServer httpErrors errorMode="
我已经尝试了以下(并尝试使用已注释掉的未注释)但只得到一个错误:

The resource you are looking for has been removed,had its name
changed,or is temporarily unavailable.

在已发布的网站项目的web.config中:

<system.webServer>
    <httpErrors errorMode="Custom"  existingResponse="Replace">
      <remove statusCode="404"/>
      <!--<error statusCode="404" responseMode="File" path="Error404.htm"/>-->
      <error statusCode="404" responseMode="ExecuteURL" path="http://example.com/Error/404.htm"/>
    </httpErrors>
  </system.webServer>

我通过将浏览器中的URL从… / default.aspx(很好)更改为… / abc.aspx来尝试.

这是重定向到错误页面的正确方法,还是有错误?

编辑

我发现如果我尝试http://example.com/nonExistingPage – 它会重定向到错误页面.但不是来自http://example.com/Folder/nonExistingPage

编辑2

通过在example.com之后指定路径可以部分解决问题.但是 – 该站点发布到example.com/subfolder,当有人导航到example.com/nonExistingFolder时 – 未显示自定义错误页面.

解决方法

在web.config中尝试这个(包括500错误支持):

<configuration>
    ...
    <system.web>
        ...
        <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Error/500.htm">
            <error statusCode="404" redirect="~/Error/404.htm" />
            <error statusCode="500" redirect="~/Error/500.htm" />
        </customErrors>
        ...
    </system.web>
    ...
    <system.webServer>
        ...
        <httpErrors errorMode="Custom">
            <remove statusCode="404" />
            <error statusCode="404" path="/Error/404.htm" responseMode="ExecuteURL" prefixLanguageFilePath="" />
            <remove statusCode="500" />
            <error statusCode="500" path="/Error/500.htm" responseMode="ExecuteURL" prefixLanguageFilePath="" />
        </httpErrors>
        ...
     </system.webServer>
     ...
</configuration>

我还建议使用.aspx页而不是.htm,以便确保在响应头中设置正确的状态代码.

<%@ Page Language="C#" %>

<% Response.StatusCode = 404; %>

<!DOCTYPE html>
<html>
<head runat="server">
    <title>404 Not Found</title>
</head>
<body>
    404 Error
</body>
</html>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读