【问题标题】:Custom error pages in web.config file are not covering all url pathsweb.config 文件中的自定义错误页面未涵盖所有 url 路径
【发布时间】:2013-05-06 03:15:56
【问题描述】:

在我的web.config 文件中,我指定了一些自定义错误:

<customErrors mode="RemoteOnly" defaultRedirect="~/Error">
   <error statusCode="500" redirect="~/Error" />
   <error statusCode="404" redirect="~/NotFound" />
</customErrors>

现在,一些链接,例如http://mysite.com/dsflhsdff 将被正确重定向到mysite.com/notfound。但是一些链接,比如https://mysite.com/videoconference/0/0/0 是由服务器本身处理的——而不是我的自定义错误页面,我得到的是 IIS 错误页面(找不到文件或目录)。在示例中,此链接 - https://scyk.pl/forums/0/0/0 将产生正确的 404 错误(我的自定义错误页面)。

这里发生了什么?我是否需要手动设置 IIS 自定义错误?如果是这样,我该怎么做?

【问题讨论】:

    标签: asp.net asp.net-mvc iis web-config


    【解决方案1】:

    这是因为 ASP.NET 甚至都不知道有 .htm 页面的请求。 IIS 将自行处理 .htm 页面,完全不涉及 ASP.NET。

    您可以通过以下两种方式之一显示您的自定义页面:

    让 ASP.NET 处理 .htm 页面:在 IIS 中右键单击您的网站/虚拟目录 --> 属性 --> 主目录/虚拟目录选项卡 --> “应用程序设置”部分下的“配置”按钮 -- > 添加映射。 为 IIS 设置自定义错误页面:在 IIS 中右键单击您的网站/虚拟目录 --> 属性 --> 自定义错误选项卡 --> 将 404 错误设置为您的错误页面。

    【讨论】:

      【解决方案2】:

      您也可以在您的 Global.asax 文件中的 Application_Error 方法中处理此错误

      例如

        void Application_Error(object sender, EventArgs e) 
          { 
              // Code that runs when an unhandled error occurs
              Exception exc = Server.GetLastError();
      
              // Handle HTTP errors
              if (exc.GetType() == typeof(HttpException))
              {
                  // The Complete Error Handling Example generates
                  // some errors using URLs with "NoCatch" in them;
                  // ignore these here to simulate what would happen
                  // if a global.asax handler were not implemented.
                  /*if (exc.Message.Contains("NoCatch") || exc.Message.Contains("maxUrlLength"))
                      return;*/
      
                  //Redirect HTTP errors to HttpError page
                  /*Server.Transfer("HttpErrorPage.aspx");*/
              }
          }
      

      更多信息: http://www.asp.net/web-forms/tutorials/aspnet-45/getting-started-with-aspnet-45-web-forms/aspnet-error-handling

      【讨论】:

        【解决方案3】:

        如果页面调用没有通过 asp.net 引擎,则由 IIS 处理。

        您可以这样做setup very easy if you have direct access to the iis,但 IIS 为您提供了设置此自定义错误的选项,也可以直接从您网站的 web.config 进行设置。以下是 404 错误的示例:

        <system.webServer>
            <httpErrors>
                <remove statusCode="404" subStatusCode="-1" />
                <error statusCode="404" prefixLanguageFilePath="" path="/PageNotFound.aspx" responseMode="ExecuteURL" />
            </httpErrors>
        </system.webServer>
        

        更多信息请访问:http://www.iis.net/configreference/system.webserver/httperrors

        【讨论】:

          【解决方案4】:

          发生这种情况是因为 IIS 在某些情况下无法找到您的自定义错误页面。这发生在我的某些扩展中,但是通过在 IIS 管理器的错误页面部分中配置我的自定义错误页面,我每次都能点击我的自定义错误页面。

          以下是如何做到这一点:

          进入您的 IIS 管理器并选择有问题的站点,在 IIS 部分下找到标题为“错误页面”的图标。

          双击该图标后,您将看到一个状态代码列表以及负责传递错误的相应文件。

          找到错误代码,在您的情况下为 404(如果它不存在,您可以通过单击“添加...”从右侧菜单中添加它),然后单击中的“编辑...”按钮右侧菜单。

          接下来,在您看到的对话框中单击“在此站点上执行 URL”单选按钮。您可以在此处提供用户将被重定向到的相对 URL。

          完成编辑对话框中的选项后,您将要再次在主错误页面痛苦中选择您的页面,只是这次单击“编辑功能设置...”

          在您看到的编辑错误页面设置中,确保选中“本地请求的详细错误和远程请求的自定义错误页面”。还要确保在“编辑错误页面”对话框的“默认页面”部分的“路径”字段中输入了您在之前的“编辑”菜单中输入的页面。最后,Path Type 必须设置为 'Execute URL'

          【讨论】:

            猜你喜欢
            • 2015-05-08
            • 1970-01-01
            • 2015-07-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-09-10
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多