我想写一个自定义异常,望各位路过的大侠指点指点迷津啊!
就这么简单:发生异常后,跳转到 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= &qu