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

标签 c# asp.net asp.net-mvc-5 custom-errors

我正在尝试为我的应用程序创建自定义错误页面,它在大多数情况下都有效,但不适用于 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也就是委托(delegate)这些请求。当 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
还有相关问题: What is the difference between customErrors and httpErrors?

关于c# - ASP MVC 5 - 403 自定义错误不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42765543/

相关文章:

javascript - 为什么 Angular 看不到我的 Controller ?

c# - 如何在正则表达式中仅接受没有空格的字母?

C# 拆分 byte[] 数组

asp.net - MVC 中不包含 'GetEnumerator' 的公共(public)定义

authentication - 如何使用 Windows 身份验证和自定义角色提供程序设置 OWIN

asp.net-mvc - 如何在 Visual Studio 2012 中使用 ASP.NET Identity 创建一个新的 MVC 5 项目?

c# - 赢得表格制图

c# - 如何读取使用 cmd.exe 运行的命令的输出

asp.net - 获取 IIS 条目的配置数据库路径(安装已停止,因为指定的路径不是有效的 Web 应用程序)

asp.net - ModelState.AddModelError 到字段的特定 ValidationMessageFor?