Visual Basic.NET 7.x (2002/03)Visual Basic 9 (2008)Visual Basic 8 (2005)XML中级开发Windows.NETVisual BasicASP.NET
在 Web.config 中处理特定异常
使用企业库处理异常。
引言
我的目标是在 web.config 中提供处理异常的最简单方法,并在发生特定异常时重定向客户端。
背景
当我浏览网络寻找在 web.config 中处理异常的方法时……我发现了企业库,它使这项任务变得简单。我们使用企业库异常处理应用程序块来
- 调查后重新抛出原始异常
- 用另一个异常替换原始异常
- 记录异常详情
使用代码
步骤 1
我创建了一个名为 ErrorHandling
的类,并将其制作成一个 DLL,以便我们可以在 ASP.NET 应用程序中直接使用它。只需将此 DLL 包含在 ASP.NET 应用程序的 bin 目录中即可。
Imports System.Web
Imports System.Collections.Specialized
Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration
Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration
<ConfigurationElementType(GetType(CustomHandlerData))> _
Public Class CustomExcepetion
Implements IExceptionHandler
Public val As String
Public Sub New(ByVal Url As NameValueCollection)
val = Url.Item("url")
End Sub
Public Function HandleException(ByVal exception As System.Exception, _
ByVal handlingInstanceId As System.Guid) _
As System.Exception Implements _
Microsoft.Practices.EnterpriseLibrary.
ExceptionHandling.IExceptionHandler.HandleException
HttpContext.Current.Response.Redirect(val)
End Function
End Class
第二步
下一步是在 ASP.NET 应用程序的 web.config 中进行更改
<configuration>
<configSections>
<section name="exceptionHandling"
type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.
Configuration.ExceptionHandlingSettings,
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling,
Version=3.1.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"/>
</configSections>
<exceptionHandling>
<exceptionPolicies>
<add name="General Policy">
<exceptionTypes>
<add type="System.Data.SqlClient.SqlException, System.Data,
Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"
postHandlingAction="None" name="SqlException">
<exceptionHandlers>
<add url="~/ErrorPage/DatabaseFailed.aspx"
type="ErrorHandling.CustomExcepetion, ErrorHandling,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
name="Custom Handler" />
</exceptionHandlers>
</add>
<add type="System.Net.WebException, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="None" name="WebException">
<exceptionHandlers>
<add url="~/ErrorPage/RemoteServerFailure.aspx"
type="ErrorHandling.CustomExcepetion, ErrorHandling,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
name="Custom Handler" />
</exceptionHandlers>
</add>
<add type="System.TimeoutException, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="None"
name="TimeoutException">
<exceptionHandlers>
<add url="~/ErrorPage/DatabaseFailed.aspx"
type="ErrorHandling.CustomExcepetion, ErrorHandling,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
name="Custom Handler" />
</exceptionHandlers>
</add>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
步骤 3
Application_Error
事件处理程序在应用程序的 Global.asax 文件中指定。
protected void Application_Error(object sender, EventArgs e)
{
Exception objErr = Server.GetLastError().GetBaseException();
ExceptionPolicy.HandleException(ex, "General Policy")
//additional actions...
}