Custom errors work for HttpCode 403 but not 500?

I am introducing custom errors in my MVC3 application, it is included in 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 properly named views:

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

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

To check, I throw exceptions in another controller:

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

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

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

500 doesn't work - instead, I am redirected to the default error page in the public folder.

Any ideas?

+5
source share
2 answers

I have 500 errors and works with httpErrors in addition to the standard customErros configuration:

  <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());

, , .

Server.GetLastError()

. fooobar.com/questions/16780/... MVC3. , .

+4

, Global.asax :

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));

        }

500:/Error/Index? code = 500

0

All Articles