Asp.net mvc 3 仅捕获特定类型异常的自定义ErrorHandling操作筛选器
我已经实现了以下操作过滤器来处理ajax错误:Asp.net mvc 3 仅捕获特定类型异常的自定义ErrorHandling操作筛选器,asp.net-mvc-3,asp.net-mvc-4,Asp.net Mvc 3,Asp.net Mvc 4,我已经实现了以下操作过滤器来处理ajax错误: public class HandleAjaxCustomErrorAttribute : ActionFilterAttribute, IExceptionFilter { public void OnException(ExceptionContext filterContext) { if (!filterContext.HttpContext.Request.IsAjaxReq
public class HandleAjaxCustomErrorAttribute : ActionFilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
if (!filterContext.HttpContext.Request.IsAjaxRequest()) return;
filterContext.Result = AjaxError(filterContext.Exception.Message, filterContext);
//Let the system know that the exception has been handled
filterContext.ExceptionHandled = true;
}
}
我希望过滤器能够仅捕获某些类型的错误,并在控制器操作中这样使用它:
[HandleAjaxCustomErrorAttribute(typeof(CustomException))]
public ActionResult Index(){
// some code
}
这怎么会发生?谢谢 我想你在找这个: 要为属性指定参数,可以创建非静态属性或使用构造函数。在你的情况下,它看起来像这样:
public class HandleAjaxCustomErrorAttribute : ActionFilterAttribute, IExceptionFilter
{
private Type _exceptionType;
public void OnException(ExceptionContext filterContext)
{
if (filterContext.Exception.GetType() != _exceptionType) return;
if (!filterContext.HttpContext.Request.IsAjaxRequest()) return;
filterContext.Result = AjaxError(filterContext.Exception.Message, filterContext);
//Let the system know that the exception has been handled
filterContext.ExceptionHandled = true;
}
public HandleAjaxCustomErrorAttribute(Type exceptionType)
{
_exceptionType = exceptionType;
}
}
我想你在找这个: 要为属性指定参数,可以创建非静态属性或使用构造函数。在你的情况下,它看起来像这样:
public class HandleAjaxCustomErrorAttribute : ActionFilterAttribute, IExceptionFilter
{
private Type _exceptionType;
public void OnException(ExceptionContext filterContext)
{
if (filterContext.Exception.GetType() != _exceptionType) return;
if (!filterContext.HttpContext.Request.IsAjaxRequest()) return;
filterContext.Result = AjaxError(filterContext.Exception.Message, filterContext);
//Let the system know that the exception has been handled
filterContext.ExceptionHandled = true;
}
public HandleAjaxCustomErrorAttribute(Type exceptionType)
{
_exceptionType = exceptionType;
}
}