0

我的 web.config :

 <customErrors mode="Off" redirectMode="ResponseRewrite" defaultRedirect="~/Pages/Error/DefaultError.aspx">
      <error statusCode="404" redirect="~/Pages/Error/Page404.aspx" />
      <error statusCode="500" redirect="~/Pages/Error/DefaultError.aspx" />
    </customErrors>

<httpErrors existingResponse="Replace" defaultResponseMode="ExecuteURL" errorMode="Custom"> //also used other options for existingResponse
    <remove statusCode="403"/>
    <error statusCode="403" path="~/Pages/Error/PI403.aspx" responseMode="ExecuteURL"/>
</httpErrors>

这是我在 global.asax.cs 文件中的 Application_error 方法:

Server.ClearError(); //clear the error so we can continue onwards

var errorPage = "~/Pages/Error/DefaultError.aspx";
var httpException = ex as HttpException;
if (httpException != null && httpException.GetHttpCode() == (int)HttpStatusCode.NotFound)
    {
        errorPage = "~/Pages/Error/Page404.aspx";
    }
Response.Redirect(errorPage);

我在项目中有日志文件,但该文件仅使用日志记录。如果我使用 myURL/Logs 浏览器链接,我会得到 IIS 403.14 错误页面。如果我编写 myURL/asdeds,我会得到我的自定义错误页面(在我的项目中不存在类似的内容)。因为403异常不会触发Application_Error。我想为所有异常显示我的自定义错误页面。当我将 myURL/Logs 写入 URL 部分时,我应该会看到我的自定义错误页面。

我还在 Application_BeginRequest 中设置了 TrySkipIisCustomError 属性

HttpContext.Current.Response.TrySkipIisCustomErrors = true;

4

2 回答 2

0

由于 403.14 - Forbidden 错误是 IIS 错误而不是 asp.net 错误。它使用 StaticFile http 处理程序而不是 asp.net http 处理程序。因此,当您遇到此错误时,不会触发 Application_error。

修改自定义错误页面的唯一方法是使用 IIS 自定义错误页面。

正如 Lex 所说,IIS 自定义错误页面不支持“~”。它将自动匹配您的 Web 应用程序根路径。所以你可以使用下面的配置设置。

<httpErrors existingResponse="Replace" defaultResponseMode="ExecuteURL" errorMode="Custom"> //also used other options for existingResponse
    <remove statusCode="403"/>
    <error statusCode="403" path="/Pages/Error/PI403.aspx" responseMode="ExecuteURL"/>
</httpErrors>

您也可以打开 IIS 管理控制台并使用 UI 窗口来修改设置。

在此处输入图像描述

于 2019-09-24T07:33:05.017 回答
0

首先你必须去IIS

  1. 像这样选择配置编辑器:

在此处输入图像描述

  1. 在打开的页面中,打开部分组合框并选择 httpErrors

在此处输入图像描述

  1. 在打开的页面中,解锁 defaultPath

在此处输入图像描述

  1. 对 IIS 上的应用程序重复步骤 1、2 和 3

在此处输入图像描述

  1. 转到应用程序的错误页面

在此处输入图像描述

  1. 选择 403 错误并进行编辑。在编辑操作页面中,您可以选择使用 302 重定向响应并在绝对 URL 文本框中键入自定义错误页面地址。 在此处输入图像描述

不要忘记,这个操作是在服务器上执行的。现在,如果向服务器发送请求(smp:https://yoursite.com/scripts)并且如果您收到错误 302,您想要的页面将打开。

于 2020-11-18T13:57:12.247 回答