ASP MVC 5 - 403 自定义错误不工作

ASP MVC 5 - 403 customError not working

我正在尝试为我的应用程序创建自定义错误页面,它在大多数情况下都有效,但不适用于 403 个错误。

我的Web.config:

<customErrors mode="On" defaultRedirect="~/Error">
  <error statusCode="404" redirect="~/Error/NotFound" />
  <error statusCode="500" redirect="~Error/InternalServer" />
  <error statusCode="403" redirect="~Error/Forbidden" />
</customErrors>

我有一个 ErrorController 正在委托这些请求。当 404 命中时,它会显示自定义错误页面,但 403 不会。我得到了默认的 IIS 403 - Forbidden 页面,即使我已经为它设置了自定义错误页面。我环顾四周并尝试使用 <httpErrors> 而不是它似乎每次都给我一个空白页。

这是我的 Global.asax 如果有帮助的话:

void Application_Error(object sender, EventArgs e)
{
    Exception exc = Server.GetLastError();

    if (exc is HttpUnhandledException)
    {
        // Pass the error on to the error page.
        Server.Transfer("ErrorPage.aspx?handler=Application_Error%20-%20Global.asax", true);
    }
}

您可以为 IIS 7+ 使用新方法。

<httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="403" />
      <remove statusCode="404" />
      <remove statusCode="500" />
      <error statusCode="403" path="/Error" responseMode="ExecuteURL" />
      <error statusCode="404" path="/Error/404" responseMode="ExecuteURL" />
      <error statusCode="500" path="/Error/500" responseMode="ExecuteURL" />
</httpErrors>

system.webServer 部分中的 httpErros 部分。

IIS 配置参考: https://www.iis.net/configreference/system.webserver/httperrors
还有相关问题: