Chris
Chris

Reputation: 27384

Custom error page isn't shown when exception thrown

I have setup a custom error handler to show diagnostic information to a user if a crash occurs. The problem is the custom error page is not shown and I get an exception (according to the webpage) thrown whilst trying to show the error page. I cannot figure out what is causing it though? I have similar pages set up for 500 and 404 errors which work fine.

The error page says Server Error in '/' Application.

Description: An exception occurred while processing your request. Additionally, another exception occurred while executing the custom error page for the first exception. The request has been terminated.

Ill show snippets of my setup, if anyone wants to see more please ask. FYI I am throwing the exception by removing the connection string details from my web.config (its an actual error we are seeing during deployment so I want to target this specifically)

<system.webServer>
  <httpErrors errorMode="Custom">
    <remove statusCode="404" />
    <error statusCode="404" path="/error/notfound" responseMode="ExecuteURL" />
    <remove statusCode="403" />
    <error statusCode="403" path="/error/forbidden" responseMode="ExecuteURL" />
    <remove statusCode="500" />
    <error statusCode="500" path="/error/" responseMode="ExecuteURL" />
  </httpErrors>
</system.webServer>

<!-- .... -->

<system.web>
  <customErrors mode="On" defaultRedirect="~/Error" redirectMode="ResponseRewrite">
    <error redirect="~/Error/NotFound" statusCode="404" />
    <error redirect="~/Error/Forbidden" statusCode="403"  />
    <error redirect="~/Error/" statusCode="500"  />
  </customErrors>
</system.web>

I then have an ErrorController with a default Index() function (no breakpoint within this controller gets hit)

public ViewResult Index()
{
    return View("Error");
}

Note: I have functions for Forbidden() and NotFound() - I just didnt copy them here - these errors work perfectly fine.

Upvotes: 1

Views: 8553

Answers (3)

Michael Cook
Michael Cook

Reputation: 1707

You cannot use redirectMode="ResponseRewrite" when redirecting to another action, you have to use redirectMode="ResponseRedirect". You will loose the ability to use Server.GetLastError(), but if you don't need that, you will be fine.

Upvotes: 0

JuhaKangas
JuhaKangas

Reputation: 883

If you don't have NotFound and Forbidden actions setup those errors are likely throwing a second exception that is going to your index page since it is the default. Trying adding those two others your controller and see if they get hit and perhaps you can find the problem easier.

Also note that ResponseRewrite isn't compatible with mvc, see the answer here: CustomErrors does not work when setting redirectMode="ResponseRewrite"

Upvotes: 1

Sergio
Sergio

Reputation: 6948

Do you have all those roots available you specified in web.config? Seems that your controller just have index action, returning some view, but it supposed to have NotFound and Forbidden actions as well, or at least correct routs such as:

routes.MapRoute(
    "NotFound",
    "Error/NotFound",
    new { controller = "Error", action = "Index" }, //bind all routes to single action
    null, controllerNamespaces
);
routes.MapRoute(
    "Forbidden",
    "Error/Forbidden",
    new { controller = "Error", action = "Index" }, //bind all routes to single action
    null, controllerNamespaces
);

Upvotes: 2

Related Questions