禁用默认的ASP.NET错误页面,显示默认的IIS错误页面(仅限远程)

时间:2017-10-08 17:33:55

标签: asp.net .net asp.net-mvc iis asp.net-mvc-5

我是否可以仅针对远程请求禁用默认的ASP.NET错误页面,而不是IIS中的错误页面?基本上HttpResponse.TrySkipIisCustomErrors但反过来。

我尝试调用Response.Clear()从响应中删除默认的ASP.NET错误页面,并使用existingResponse="PassThrough"配置我的IIS错误页面。虽然这会阻止显示默认的ASP.NET错误页面,但它不会显示IIS错误页面。 (I've opened a separate question about PassThrough

1 个答案:

答案 0 :(得分:0)

第一个解决方案:  对于IIS 7+,将使用其中定义错误页面行为的标记,而不是每页/请求,而是全局地为web.config中的应用程序定义,如下所示:

<system.webServer>
  <httpErrors errorMode="Custom"/>
  [...]
</system.webServer>

第二解决方案: 禁用此行为是在错误控制器中设置Response.TrySkipIisCustomErrors,如下所示:

[HandleError]
public class ErrorController : Controller
{
  public ActionResult NotFound()
  {
    Response.TrySkipIisCustomErrors = true;
    Response.StatusCode = (int)HttpStatusCode.NotFound;
    return View("NotFound");
  }

}