I built a custom Exception and I'm throwing an instance of this class whenever I consider that the my application is in an 'exceptional state'. This exception is being thrown in an application logic class (service in my case). Now, I also have the default ASP MVC exception redirection in place, but in case of these custom exceptions, I don't want it to redirect to the 'Error' page, but to show a message in a div (for example in my Layout.cshtml page). I was thinking on adding the exception message to TempData["ApplicationError"]. I want to write this code only once, not all over my controller action methods. So, in my base controller class I overridden protected void OnException(ExceptionContext filterContext) and my code looks like this:
protected override void OnException(ExceptionContext filterContext)
{
if (filterContext.Exception is InvalidOperationException)
TempData["ApplicationError"] = filterContext.Exception.Message;
else
base.OnException(filterContext);
}
My problem is that I still get redirected to the default error page when this exception is thrown. I DON'T want this to happen. What I want is to display the custom exception's message in a friendly div on the same view the user is on. Do you have any ideas on how can I achieve this?