ASP.Net异常的捕获小结
时间:2010-10-18 来源:KymoWang
- 建立一个PageBase (class),自定义OnError事件
- 在Global.asax中自定义Application_Error事件
- 在web.config中定义新的页面
<customErrors mode="On" defaultRedirect="~/errors/GeneralError.aspx"/>
自定义GeneralError.aspx捕获异常 - 在HttpModule中自定义OnError事件
例子:
protected override void OnError(EventArgs e) { // At this point we have information about the error HttpContext ctx = HttpContext.Current; Exception exception = ctx.Server.GetLastError (); string errorInfo = "
Offending URL: " + ctx.Request.Url.ToString () + "
Source: " + exception.Source + "
Message: " + exception.Message + "
Stack trace: " + exception.StackTrace; ctx.Response.Write (errorInfo); // -------------------------------------------------- // To let the page finish running we clear the error // -------------------------------------------------- ctx.Server.ClearError (); base.OnError (e); }
相关阅读 更多 +