如何显示自定义错误页而不是iis 403错误页?

如何显示自定义错误页而不是iis 403错误页?,iis,global-asax,custom-errors,application-error,Iis,Global Asax,Custom Errors,Application Error,my web.config: <customErrors mode="Off" redirectMode="ResponseRewrite" defaultRedirect="~/Pages/Error/DefaultError.aspx"> <error statusCode="404" redirect="~/Pages/Error/Page404.aspx" /> <error statusCode="500" redirect="~

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

我还在应用程序_BeginRequest中设置了TrySkipIisCustomError属性


HttpContext.Current.Response.tryskipiiscustomerors=true

因为403.14禁止的错误是IIS错误而不是asp.net错误。它使用StaticFile http处理程序,而不是asp.net http处理程序。因此,当您遇到此错误时,不会触发应用程序错误

修改自定义错误页的唯一方法是使用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>
//还为existingResponse使用了其他选项
您还可以打开IIS管理控制台并使用UI窗口修改设置


首先,您必须转到IIS

  • 选择如下所示的配置编辑器:
  • 在打开的页面中,打开节组合框并选择httpErrors
  • 在打开的页面中,解锁defaultPath
  • 对IIS上的应用程序重复步骤1、2和3
  • 转到应用程序的错误页面
  • 选择403错误并编辑它。在编辑操作页面中,您可以选择Respond with a 302 redirect,并在绝对URL文本框中键入自定义错误页面地址。

  • 不要忘记,此操作是在服务器上执行的。现在,如果请求发送到服务器(smp:https://yoursite.com/scripts),并且出现错误302,则将打开所需的页面。

    ~
    如果您阅读了这些配置元素的文档,则该页面无效。
    <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>