Redirect In HandleErrorAttribute not work

171 Views Asked by At

I want redirect the user in case of specific exception.

for this i have written custom Exception-Filter, and it works, meaning if I put a breakpoint it goes through it properly, but i can not override the normal behavior of the Exception and move the user to a custom page.

The Exception Simulate:

public ActionResult Index()
{
    //test exception throw 
    throw new ArgumentNullException();
    return View();
}

The Filter Redirect:

public class CustomExceptionHandlerAttribute : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        if (filterContext.Exception is ArgumentNullException)
            //breakpoint here work as expected
            filterContext.Result = new RedirectToRouteResult( 
                new RouteValueDictionary(new { controller = "Home", action = "About" })
            );
        else
            base.OnException(filterContext);
    }
}

The Result in browser (also without debugging):

Server Error in '/' Application.

Value cannot be null or empty.

0

There are 0 best solutions below