当前位置: 代码迷 >> Ruby/Rails >> 替HttpStatusCodeResult添加customErrors
  详细解决方案

替HttpStatusCodeResult添加customErrors

热度:184   发布时间:2016-04-29 02:24:43.0
为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);    }}