为HttpStatusCodeResult加入customErrors

asp.net mvc的action返回值为HttpStatusCodeResult时的customErrors总是不起作用

(404和exception时的500,因为他们并不是HttpStatusCodeResult类型的返回值,所以还能使用)

其解决的方法例如以下:

1. 开启customErrors(为了保留原有的404、500等错误)

2. 开启httpErrors(为专门定制的statuscode)

3. 设置Global:Response.TrySkipIisCustomErrors = true;

  <system.web>
    <customErrors mode="On" defaultRedirect="~/Home/Error">
      <error statusCode="404" redirect="~/Home/Error/404" />
      <error statusCode="500" redirect="~/Home/Error/500" />
    </customErrors>
  </system.web>
  <system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Auto" defaultResponseMode="ExecuteURL">
      <error statusCode="420" path="/Home/Error/420" responseMode="ExecuteURL" />
    </httpErrors>
  </system.webServer>


 

protected void Application_Error(object sender, EventArgs e)
{

    Response.TrySkipIisCustomErrors = true;

    Exception exc = Server.GetLastError();
    if (exc is HttpUnhandledException)
    {
        Server.Transfer("Home/Error/500", true);
    }
}


 

你可能感兴趣的:(result)