显示“无法找到资源”错误的自定义错误页面

时间:2013-06-19 13:31:39

标签: c# asp.net exception

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

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

3 个答案:

答案 0 :(得分:4)

您需要使用代码404处理服务器错误。网络上有大量示例(onetwothree),但简而言之,您可以在web.config包含以下部分:

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

其中~/Error.html~/Error404.html是“资源无法找到”案例的常规错误页面和页面。

答案 1 :(得分:2)

将以上内容添加到您的webconfig

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

答案 2 :(得分:1)

查看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>