Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Errors works for HttpCode 403 but not 500?

I am implementing custom errors in my MVC3 app, its switched on in the web.config:

<customErrors mode="On">
  <error statusCode="403" redirect="/Errors/Http403" />
  <error statusCode="500" redirect="/Errors/Http500" />
</customErrors>

My controller is very simple, with corresponding correctly named views:

public class ErrorsController : Controller
{
    public ActionResult Http403()
    {
        return View("Http403");
    }

    public ActionResult Http500()
    {
        return View("Http500");
    }
}

To test, I am throwing exceptions in another controller:

public class ThrowingController : Controller
{
    public ActionResult NotAuthorised()
    {
        throw new HttpException(403, "");
    }

    public ActionResult ServerError()
    {
        throw new HttpException(500, "");
    }
}

The 403 works - I get redirected to my custom "/Errors/Http403".

The 500 does not work - I instead get redirected to the default error page in the shared folder.

Any ideas?

like image 418
Nick Avatar asked Oct 09 '22 01:10

Nick


1 Answers

I've got 500 errors up and running by using the httpErrors in addition to the standard customErros config:

  <system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="403" subStatusCode="-1" />
      <error statusCode="403" path="/Errors/Http403" responseMode="ExecuteURL" />
      <remove statusCode="500" subStatusCode="-1" />
      <error statusCode="500" path="/Errors/Http500" responseMode="ExecuteURL" />
    </httpErrors>
  </system.webServer>

And removing this line from global.asax

GlobalFilters.Filters.Add(new HandleErrorAttribute());

Its not perfect however as I'm trying to retrieve the last error which is always null.

Server.GetLastError()

See https://stackoverflow.com/a/7499406/1048369 for the most comprehensive piece on custom errors in MVC3 I have found which was of great help.

like image 160
Nick Avatar answered Oct 12 '22 10:10

Nick