Exception not being passed to my Error Page in MVC4

191 views Asked by At

I've got my customErrors mode set to On and redirecting to:

enter image description here

Here's my view in my solution:

enter image description here

Whenever I have an unhandled exception, my app redirects me to the Error page, as expected. But, I lose the exception information, because, I'm assuming, the app is redirecting. I even tried redirectMode="ResponseRewrite" in the customErrors element, but that didn't work.

So, here's my "Index" view on my Error controller, which the app is redirecting to- notice the model:

enter image description here

And further down, I have:

enter image description here

But, @Model is null. How can I get the exception information from a customErrors redirect?

1

There are 1 answers

9
Laurent On BEST ANSWER

To get that model, you need to use the HandleErrorAttribute filter to catch the error. You can see instructions here: http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/handleerrorattribute-or-error-handling-in-mvc-4/

TL;DR:

Disable the redirect, but make sure custom errors are still on:

<customErrors mode="On"/>

Make sure the HandleErrorAttribute filter is registered globally (it is by default):

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
}

Move/rename your error view to ~\Views\Shared\Error.cshtml