我想写一个自定义异常,望各位路过的大侠指点指点迷津啊!
就这么简单:发生异常后,跳转到 Error.aspx,同时显示自定义的对应出错信息!
[解决办法]
在httpmodule的错误事件上处理吧,捕获后统一处理。
[解决办法]
1
实现ihttpmodule
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Security;
namespace DuwCompontents
{
public class DuwHttpModule : IHttpModule
{
#region IHttpModule Members
void IHttpModule.Dispose()
{
}
void IHttpModule.Init(HttpApplication context)
{
context.Error += new EventHandler(context_Error);
}
#endregion
void context_Error(object sender, EventArgs e)
{
//统一错误处理
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
DuwException excep = context.Server.GetLastError().InnerException as DuwException;
if (excep == null) {
string message=context.Server.GetLastError().InnerException.StackTrace;
message.Replace( "\r ", " <br/> ");
excep = new DuwException(message,DuwExceptionType.UndefinedException);
}
string errorurl=String.Format( "/error.aspx?type={0}&message={1}&returnurl={2} ",excep.ExceptionType,context.Server.UrlEncode(excep.Message),context.Server.UrlEncode(excep.ReturnURL));
context.Server.Transfer(errorurl);
}
}
}
2自定义异常类
其中1中的DuwException就是自己定义的异常类
定义如下
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace DuwCompontents
{
/// <summary>
/// 异常类
/// </summary>
public class DuwException : ApplicationException
{
#region 私有属性
private DuwExceptionType _Type;
private string _ReturnURL;
#endregion
#region 公有属性
public string ReturnURL{get { return _ReturnURL; }}
public DuwExceptionType ExceptionType{get{return _Type;}}
#endregion
#region 私有方法
private void Init(DuwExceptionType type)
{
string returnurl = HttpContext.Current.Request.RawUrl;
if (HttpContext.Current.Request.UrlReferrer != null)
{
returnurl = HttpContext.Current.Request.UrlReferrer.ToString();
}
Init(type, returnurl);
}
private void Init(DuwExceptionType type, string returnurl)
{
_Type = type;
_ReturnURL = returnurl;
}
#endregion
#region 构造
/// <summary>
/// 构造
/// </summary>
/// <param name= "message "> 错误提示信息 </param>
public DuwException(string message) : base(message) {
Init(DuwExceptionType.UndefinedException);
}
/// <summary>
/// 构造
/// </summary>
/// <param name= "message "> 错误提示信息 </param>
/// <param name= "type "> 错误类型 </param>
public DuwException(string message, DuwExceptionType type)
: base(message)
{
Init(type);
}
/// <summary>
/// 构造
/// </summary>
/// <param name= "message "> 错误提示信息 </param>
/// <param name= "returnurl "> 指定返回页面 </param>
public DuwException(string message,string returnurl):base(message){
Init(DuwExceptionType.UndefinedException, returnurl);
}
/// <summary>
/// 构造
/// </summary>
/// <param name= "message "> 错误提示信息 </param>
/// <param name= "type "> 错误类型 </param>
/// <param name= "returnurl "> 指定返回页面 </param>
public DuwException(string message, DuwExceptionType type, string returnurl):base(message)
{
Init(type, returnurl);
}
#endregion
}
}
DuwExceptionType是自己定义的错误类型枚举.你可以自由定义,偶的和你的可就不一样了
3 修改web.config
<httpModules>
<add name= "DuwHttpModule " type= "DuwCompontents.DuwHttpModule,DuwCompontents "/>
</httpModules>
加入配置节DuwHttpModule ,其中type指明模块和第一步的类全名