ASP MVC 5-403 customError不起作用

Tob*_*oby 3 c# asp.net custom-errors asp.net-mvc-5

我正在尝试为我的应用程序创建自定义错误页面,它在大多数情况下都起作用,但对于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>
Run Code Online (Sandbox Code Playgroud)

我有一个ErrorController委托这些请求。当404点击,它会显示自定义错误页,但403没有。403 - Forbidden即使为它设置了自定义错误页面,我仍在获取默认的IIS 页面。我环顾四周,尝试使用<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);
    }
}
Run Code Online (Sandbox Code Playgroud)

bru*_*rut 7

您可以对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>
Run Code Online (Sandbox Code Playgroud)

system.webServer部分中的httpErros部分。

IIS配置参考:https ://www.iis.net/configreference/system.webserver/httperrors
以及相关问题: customErrors和httpErrors有什么区别?