![](/img/trans.png)
[英]Serilog stops logging exceptions as soon as I add a custom ExceptionFilter
[英]How to allow custom ExceptionFilter to continue to call the designated Action Method during exceptions
当我的异常过滤器被调用时,我希望控制器中的预期动作仍然被调用。 我创建了以下IExceptionFilter:
public class ArgumentExceptionFilter : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
if (filterContext.Exception.GetType() == typeof(System.ArgumentException))
{
//Some logic to create a default "SettingsRoot" parameter
//This simply surpresses MVC from raising exception
filterContext.ExceptionHandled = true;
}
}
}
并将此过滤器应用于此控制器操作方法:
[ArgumentExceptionFilter]
public ActionResult MyActionMethod(SettingsRoot settings)
{
ActionResult actionResult = null;
//Do stuff with settings
return actionResult;
}
我希望有“ MyActionMethod()”被调用,无论我们是否得到触发ExceptionFilter的异常。 我也尝试使用“ RedirectToRouteResult()”方法,但是没有用。 有什么建议么?
您可以使用filterContext的Result属性使用默认参数重定向到控制器操作。
public void OnException(ExceptionContext filterContext)
{
if (filterContext.Exception.GetType() == typeof(System.ArgumentException))
{
//This simply surpresses MVC from raising exception
filterContext.ExceptionHandled = true;
//Some logic to create a default "SettingsRoot" parameter
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary
{
{ "controller", "Default" },
{ "action", "MyActionMethod" },
{ "settings", new SettingsRoot() }
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.