<!--自定义异常配置-->
<settingException>
<exceptions>
<!--add优先级高于group-->
<add exception="Exceptions.PasswordErrorException"
view ="PasswordErrorView"
handler="ExceptionHandlers.PasswordErrorExceptionHandler"/>
<groups>
<!--group可以配置一种异常的view和handler-->
<group view="EmptyErrorView" handler="ExceptionHandlers.EmptyExceptionHandler">
<add exception="Exceptions.UserNameEmptyException"/>
<add exception="Exceptions.EmailEmptyException"/>
</group>
</groups>
</exceptions>
</settingException>
public class ExceptionConfig
{
/// <summary>
/// 视图
/// </summary>
public string View{get;set;}
/// <summary>
/// 异常对象
/// </summary>
public Exception Exception{get;set;}
/// <summary>
/// 异常处理程序
/// </summary>
public IExceptionHandler Handler{get;set;}
}
public interface IExceptionHandler
{
/// <summary>
/// 异常是否处理完成
/// </summary>
bool HasHandled{get;set;}
/// <summary>
/// 处理异常
/// </summary>
/// <param name="ex"></param>
void Handle(Exception ex);
}
public class SettingHandleErrorFilter : IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
if(filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
ExceptionConfig config = SettingExceptionProvider.Container[filterContext.Exception.GetType()];
if(config == null)
{
return;
}
if(config.Handler != null)
{
//执行Handle方法
config.Handler.Handle(filterContext.Exception);
if (config.Handler.HasHandled)
{
//异常已处理,不需要后续操作
filterContext.ExceptionHandled = true;
return;
}
}
//否则,如果有定制页面,则显示
if(!string.IsNullOrEmpty(config.View))
{
//这里还可以扩展成实现IView的视图
ViewResult view = new ViewResult();
view.ViewName = config.View;
filterContext.Result = view;
filterContext.ExceptionHandled = true;
return;
}
//否则将异常继续传递
}
}
public class SettingExceptionProvider
{
public static Dictionary<Type, ExceptionConfig> Container =
new Dictionary<Type, ExceptionConfig>();
static SettingExceptionProvider()
{
InitContainer();
}
//读取配置信息,初始化容器
private static void InitContainer()
{
var section = WebConfigurationManager.GetSection("settingException") as SettingExceptionSection;
if(section == null)
{
return;
}
InitFromGroups(section.Exceptions.Groups);
InitFromAddCollection(section.Exceptions.AddCollection);
}
private static void InitFromGroups(GroupCollection groups)
{
foreach (var group in groups.Cast<GroupElement>())
{
ExceptionConfig config = new ExceptionConfig();
config.View = group.View;
config.Handler = CreateHandler(group.Handler);
foreach(var item in group.AddCollection.Cast<AddElement>())
{
Exception ex = CreateException(item.Exception);
config.Exception = ex;
Container[ex.GetType()] = config;
}
}
}
private static void InitFromAddCollection(AddCollection collection)
{
foreach(var item in collection.Cast<AddElement>())
{
ExceptionConfig config = new ExceptionConfig();
config.View = item.View;
config.Handler = CreateHandler(item.Handler);
config.Exception = CreateException(item.Exception);
Container[config.Exception.GetType()] = config;
}
}
//根据完全限定名创建IExceptionHandler对象
private static IExceptionHandler CreateHandler(string fullName)
{
if(string.IsNullOrEmpty(fullName))
{
return null;
}
Type type = Type.GetType(fullName);
return Activator.CreateInstance(type) as IExceptionHandler;
}
//根据完全限定名创建Exception对象
private static Exception CreateException(string fullName)
{
if(string.IsNullOrEmpty(fullName))
{
return null;
}
Type type = Type.GetType(fullName);
return Activator.CreateInstance(type) as Exception;
}
}
/// <summary>
/// settingExceptions节点
/// </summary>
public class SettingExceptionSection : ConfigurationSection
{
[ConfigurationProperty("exceptions",IsRequired=true)]
public ExceptionsElement Exceptions
{
get
{
return (ExceptionsElement)base["exceptions"];
}
}
}
/// <summary>
/// exceptions节点
/// </summary>
public class ExceptionsElement : ConfigurationElement
{
private static readonly ConfigurationProperty _addProperty =
new ConfigurationProperty("", typeof(AddCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);
[ConfigurationProperty("", IsDefaultCollection = true)]
public AddCollection AddCollection
{
get
{
return (AddCollection)base[_addProperty];
}
}
[ConfigurationProperty("groups")]
public GroupCollection Groups
{
get
{
return (GroupCollection)base["groups"];
}
}
}
/// <summary>
/// group节点集
/// </summary>
[ConfigurationCollection(typeof(GroupElement),AddItemName="group")]
public class GroupCollection : ConfigurationElementCollection
{
/*override*/
protected override ConfigurationElement CreateNewElement()
{
return new GroupElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return element;
}
}
/// <summary>
/// group节点
/// </summary>
public class GroupElement : ConfigurationElement
{
private static readonly ConfigurationProperty _addProperty =
new ConfigurationProperty("", typeof(AddCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);
[ConfigurationProperty("view")]
public string View
{
get
{
return base["view"].ToString();
}
}
[ConfigurationProperty("handler")]
public string Handler
{
get
{
return base["handler"].ToString();
}
}
[ConfigurationProperty("", IsDefaultCollection = true)]
public AddCollection AddCollection
{
get
{
return (AddCollection)base[_addProperty];
}
}
}
/// <summary>
/// add节点集
/// </summary>
public class AddCollection : ConfigurationElementCollection
{
/*override*/
protected override ConfigurationElement CreateNewElement()
{
return new AddElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return element;
}
}
/// <summary>
/// add节点
/// </summary>
public class AddElement : ConfigurationElement
{
[ConfigurationProperty("view")]
public string View
{
get
{
return base["view"] as string;
}
}
[ConfigurationProperty("handler")]
public string Handler
{
get
{
return base["handler"] as string;
}
}
[ConfigurationProperty("exception", IsRequired = true)]
public string Exception
{
get
{
return base["exception"] as string;
}
}
}
public class PasswordErrorException : Exception{}
public class UserNameEmptyException : Exception{}
public class EmailEmptyException : Exception{}
public class PasswordErrorExceptionHandler : IExceptionHandler
{
public bool HasHandled{get;set;}
public void Handle(Exception ex)
{
//具体处理逻辑...
}
}
public class EmptyExceptionHandler : IExceptionHandler
{
public bool HasHandled { get; set; }
public void Handle(Exception ex)
{
//具体处理逻辑...
}
}
public ActionResult Index()
{
throw new PasswordErrorException();
}
public ActionResult Index2()
{
throw new UserNameEmptyException();
}
public ActionResult Index3()
{
throw new EmailEmptyException();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有