When an exception is thrown in one of my controllers, I'm catching it in my base class with OnException and I would like to pass the exception object to the index action of my ErrorController to display in a view.
In the example below I'm using TempData which ends up getting discarded before it reaches my ErrorController.
I know TempData only lasts until the next request but why isn't this making it that far?
I'm also open to other ways of solving this.
Test Controller
public class TestController : BaseController
{
public ActionResult Index()
{
throw new Exception("test");
}
}
Base Controller
public class BaseController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
if (filterContext.ExceptionHandled)
return;
filterContext.ExceptionHandled = true;
// Redirect to a different controller than the one that threw the exception
filterContext.Result = RedirectToAction("Index", "Error");
filterContext.Controller.TempData["exception"] = filterContext.Exception;
}
}