Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

I've got the same problem, I catch the Exception directly in Global.asax in that case:

protected void Application_Error(object sender, EventArgs e)
        {
            Exception exception = Server.GetLastError();

            Response.Clear();



            HttpException httpException = exception as HttpException;

            var code = httpException == null ? 500 : httpException.GetHttpCode();

            // Log the exception.
            if (code == 500)
                logError.Error(exception);

            Server.ClearError();

            Context.Items["error"] = code;

            RouteData routeData = new RouteData();
            routeData.Values.Add("controller", "Error");
            routeData.Values.Add("action", "Index");
            routeData.Values.Add("code", code);

            IController errorController = new ErrorController();
            errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));

        }

That redirects to my custom Error 500: /Error/Index?code=500

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top