Custom Errors works for HttpCode 403 but not 500?

99封情书 提交于 2019-12-10 14:36:24

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/10248700/custom-errors-works-for-httpcode-403-but-not-500

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!