1

In my MVC application I have enabled the custom errors in the web.config. I want all errors to redirect to one page.

<customErrors mode="On" redirectMode="ResponseRewrite" />

This redirects to the ~/Views/Shared/Error.cshtml page just fine when I do

throw new Exception();

But when I surf to site/Home/Bla it redirects me to an IIS general 404 error page and not my custom one.

I tried to add

private void Application_Error(object sender, EventArgs e)
{
    Response.TrySkipIisCustomErrors = true;
}

to the Global.asax, but to no avail.

Does anybody know what might be the problem? (If you need more information, then aks and ye shall receive.)

MikeSmithDev
  • 15,731
  • 4
  • 58
  • 89
Ken Bonny
  • 729
  • 1
  • 9
  • 29
  • Here's an approach using a generic error controller: http://stackoverflow.com/questions/13905164/how-to-make-custom-error-pages-work-in-asp-net-mvc-4?rq=1 – Matt Evans Jan 10 '14 at 17:01

2 Answers2

1

Try adding this to your web.config

<customErrors mode="On" />

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <!-- Avoid IIS messing with custom errors http://stackoverflow.com/a/2345742/426840 -->
    <httpErrors existingResponse="PassThrough" />

  </system.webServer>
xspydr
  • 3,030
  • 3
  • 31
  • 49
1

I have found the problem. Thanks to Jason to point me in the right direction. :) Thanks to him I got on the right track to find it.

Answer found in StackOverflow page

Apparently I pointed to a controller and an action and if you specify redirectMode="ResponseRewrite" on the customErrors node, then the redirect uses an older library to get the content. That should be a page and can't be a mvc route.

So either you specify an actual page or you use the redirectMode="ResponseRedirect" option. The page needs to be a .htm or .html page, a .cshtml isn't accepted either.

Community
  • 1
  • 1
Ken Bonny
  • 729
  • 1
  • 9
  • 29
  • +1: Thanks for posting your solution. I wish more people would do that. – John H Jan 11 '14 at 19:12
  • 1
    I know, bugs me too. That's why I make sure that when I post a question, I check back until I get the answer and update it. And I'm not afraid to give credit where it's due. :) – Ken Bonny Jan 13 '14 at 07:41