码迷,mamicode.com
首页 > Web开发 > 详细

Asp.net 未处理异常

时间:2015-08-13 15:40:14      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

  1. 页面级捕获未处理异常 - Page 的 Error 事件
    1     Protected Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Error
    2         Response.Redirect("errorInfo.aspx?ErrMsg=Unhandled&Detail=" & System.Web.HttpUtility.UrlEncode(Server.GetLastError.ToString))
    3     End Sub

    在Page的Error事件处理函数Page_Error中编写处理逻辑。

  2. 页面级捕获未处理异常 - Page 的 ErrorPage 属性
    1     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2         Me.ErrorPage = "errorInfo.aspx"
    3     End Sub

    要让ErrorPage属性生效需要在Web.config中将customErrors 的 mode 设置为On

    1 <configuration>
    2   <system.web>
    3     <customErrors mode="On"/>
    4   </system.web>
    5 </configuration>

    3. 应用程序级捕获未处理异常 - Web.config 

    1 <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
    2   <error statusCode="403" redirect="NoAccess.htm" />
    3   <error statusCode="404" redirect="FileNotFound.htm" />
    4 </customErrors>

    通过设置customErrors 节点来捕获未处理异常,mode属性有以下三种预设值,

      • Off - 禁用自定义错误信息,本地和远程用户都会看到详细的错误信息,即黄页。
      • RemoteOnly - 表示本地用户将看到详细错误信息,而远程用户将会看到自定义错误信息。只对不在本地 Web 服务器上运行的用户显示自定义(友好的)信息。出于安全目的,建议使用此设置,以便不向远程客户端显示应用程序的详细信息。
      • On - 表示在本地和远程用户都会看到自定义错误信息(友好的)。

  4. 应用程序级捕获未处理异常 - Global.asax

    

1     Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
2          Code that runs when an unhandled error occurs
3         Response.Redirect("errorInfo.aspx?ErrMsg=Unhandled&Detail=" & System.Web.HttpUtility.UrlEncode(Server.GetLastError.ToString))
4     End Sub

 

.NET提供了四种错误处理机制,它们有一定的优先级 顺序:Page_Error事件 > ErrorPage属性 > Application_Error事件 >   <customErrors>配置项 。

Asp.net 未处理异常

标签:

原文地址:http://www.cnblogs.com/irresistable/p/4726372.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!