【问题标题】:Displaying Custom Error Page for "The resource cannot be found" Error [duplicate]显示“找不到资源”错误的自定义错误页面 [重复]
【发布时间】:2013-06-16 01:35:15
【问题描述】:

我正在用 ASP.NET 编写一个应用程序。

我想知道如何在用户键入我的项目中不存在的页面地址时显示自定义错误页面。请问这怎么做?谢谢:)

【问题讨论】:

    标签: c# asp.net exception


    【解决方案1】:

    将以上内容添加到您的 webconfig 中

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

    【讨论】:

      【解决方案2】:

      查看 web.config 架构的 the MSDN documentation page for the customErrors element - 提供的示例如下:

      <configuration>
        <system.web>
          <customErrors defaultRedirect="GenericError.htm" mode="RemoteOnly">
            <error statusCode="500" redirect="InternalError.htm"/>
          </customErrors>
        </system.web>
      </configuration>
      

      【讨论】:

        【解决方案3】:

        您需要使用代码 404 处理服务器错误。网络上有很多示例(onetwothree),但简而言之,您可以在 web.config 中使用以下内容执行此操作部分:

        <customErrors mode="On" defaultRedirect="~/Error.html">
            <error statusCode="404" redirect="~/Error404.html"/>
        </customErrors>
        

        其中~/Error.html~/Error404.html 是一般错误页面和“找不到资源”情况的页面。

        【讨论】:

        猜你喜欢
        • 2011-03-25
        • 1970-01-01
        • 2013-09-12
        • 1970-01-01
        • 1970-01-01
        • 2020-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多