如何使用HandleErrorAttribute OnException事件忽略异常

时间:2010-03-16 20:49:24

标签: asp.net-mvc exception-handling

我想在一些HandleErrorAttribute类中管理所有异常。 但对于某些特定的异常类型我想忽略或取消handeling的异常 并且继续使用父请求 ..

感谢

2 个答案:

答案 0 :(得分:0)

对于那些您希望忽略的特定类型或类型的异常,不要将ExceptionHandled标志设置为true。

顺便说一下,你必须创建一个自定义的ErrorHandler。它可能是这样的(伪代码):

public class CustomHandleErrorAttribute : FilterAttribute, IExceptionFilter
{
    public void OnException(ExceptionContext filterContext)
    {
        //filterContext.ExceptionHandled = exception_is_of_type_that_must_be_handled;
        //Other code, logging, etc            
    } 
}

答案 1 :(得分:0)

我认为忽略特定异常并继续使用action方法的唯一方法是捕获控制器中的特定异常并忽略它。

E.g。

[HandleErrorAttribute]
public ActionResult YourActionMethod()
{
    try
    {
        //Do some stuff
    }
    catch (SpecificExceptionToIgnore ex)
    {
        //Do something here with the exception
        //E.g. Simply ignore it, Log it, set some modelstate or tempdata
    }

    //Carry on.
    //All other exceptions will be thrown as normal and
    //will be handled by your 'HandleErrorAttribute' attribute.

    return View();
}

HTHS,
查尔斯