1

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?

Mike Marks
  • 10,017
  • 17
  • 69
  • 128

1 Answers1

2

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

Laurent
  • 3,798
  • 25
  • 22
  • I did this, but it's not sending me to the shared Error.cshtml page - it's returning the typical "Server error in /***** Application" yellow screen of death. – Mike Marks Dec 10 '13 at 20:01
  • And I'll also say that I set a breakpoint in FilterConfig.cs (RegisterGlobalFilters), and it never gets hit. I cleaned my solution, started from scratch, it doesn't get hit. – Mike Marks Dec 10 '13 at 20:09
  • Are you certain the filter is registered globally? In ASP.NET MVC 4+, the `RegisterGlobalFilters` method should be defined in `App_Start\FilterConfig.cs` and in Global.asax.cs, there should be a call to it in `Application_Start`, like this: `FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);` – Laurent Dec 10 '13 at 20:09
  • Yes, I checked, everything checks out like you say. :( – Mike Marks Dec 10 '13 at 20:10
  • Is `Application_Start` being called at all? If not, the app might not be restarting properly... – Laurent Dec 10 '13 at 20:13
  • No, it is not. I will investigate. Thanks for all your help – Mike Marks Dec 10 '13 at 20:31
  • Sometimes, the IIS app pool is kept active. You can try killing it manually to make sure it is restarted and that Application_Start is called. – Laurent Dec 10 '13 at 20:34
  • Will HTTP errors (404, etc.) be routed to my Error page, or is it just exceptions? – Mike Marks Dec 11 '13 at 13:13
  • Unfortunately, no. To handle a 404, you could use a custom error (``), but then it's a redirect, so you lose information. For alternatives, check out the answers to this other question (beware, some are old answers for ASP.NET MVC 1 or 2): http://stackoverflow.com/questions/619895/how-can-i-properly-handle-404-in-asp-net-mvc – Laurent Dec 12 '13 at 21:06