5

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?

Nick
  • 6,366
  • 5
  • 43
  • 62

2 Answers2

4

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.

Community
  • 1
  • 1
Nick
  • 6,366
  • 5
  • 43
  • 62
0

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

fmgp
  • 1,638
  • 17
  • 17
  • I can't accept this as the answer just yet as I have got it working without using the Application_Error method and using the httpErrors config section instead. I'd give your answer a +1 but I don't have enough rep yet! Cheers tho! – Nick Apr 20 '12 at 16:46
  • Of course there is no reason httpErrors are not working (fine configured), the solution I use give me more control on uncatched exceptions – fmgp Apr 20 '12 at 16:52
  • It's invalid logic to state that if httpException is null then an assumption should be made that it's a 500 error. A 500 error must be confirmed by data passed by the exception. – Chris Halcrow Feb 05 '16 at 04:04