When my Exception Filter is called, I'd like the intended action within my controller to still be called. I had created the following 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;
}
}
}
and this filter is applied to this controller action method:
[ArgumentExceptionFilter]
public ActionResult MyActionMethod(SettingsRoot settings)
{
ActionResult actionResult = null;
//Do stuff with settings
return actionResult;
}
I would like to have "MyActionMethod()" to be called regardless if we get an exception that triggers the ExceptionFilter. I also tried to use "RedirectToRouteResult()" approach but that didn't work. Any suggestions?
You can use the Result property of the filterContext to redirect to your controller action with a default parameter.
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() }
});
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.