web.config未将非.aspx页面重定向到404错误页。

12

目标

我希望所有缺失页面的URL都能重定向到我的根目录下名为404error.aspx的404错误页面。

问题

目前只有带有.aspx的URL才有效。例如,如果您输入4error.aspx,则会重定向到错误页面。

/404error.aspx?aspxerrorpath=/4error.aspx

背景

我不是 .NET 开发者,所以我接手了这个使用经典的 .aspx 的项目。因此,我没有使用任何微软产品来构建模板或框架。我只是在 Sublime 文本编辑器中编写代码。

我的研究

我查看了很多在 Google 上找到的文章,但没有一个完整的实例能够正常工作。

代码

这是我在 web.config 中开始使用的全部代码:

web.config

<configuration>
  <system.web>
    <customErrors mode="RemoteOnly" defaultRedirect="~/404error.aspx" />
      <globalization
        fileEncoding="utf-8"
        requestEncoding="utf-8"
        responseEncoding="utf-8"
        culture="en-US"
        uiCulture="de-DE"
      />
  </system.web>
</configuration>

我还在Microsoft的这个例子中找到了一个(404error.aspx是我的修改)
http://msdn.microsoft.com/en-us/library/vstudio/bb397417%28v=vs.100%29.aspx

web.config(2)

<configuration>
  <appSettings/>
  <connectionStrings/>
  <system.web>
    <compilation debug="true" />

    <!-- Turn on Custom Errors -->
    <customErrors mode="On" 
      defaultRedirect="/404error.aspx">
      <error statusCode="404" redirect="/404Error.aspx"/>
    </customErrors>

  </system.web>
</configuration>

这也无法处理没有.aspx的页面。

然后我尝试了来自以下网页的示例:

web.config (3)

<?xml version="1.0"?>
<configuration>
   <system.web>
    <customErrors mode="RemoteOnly" defaultRedirect="~/404error.aspx" />
    <error statusCode="404" redirect="~/404error.aspx" />
    <globalization
      fileEncoding="utf-8"
      requestEncoding="utf-8"
      responseEncoding="utf-8"
      culture="en-US"
      uiCulture="de-DE"
    />
   </system.web>
   <system.webServer>
      <httpErrors>
        <remove statusCode="401" subStatusCode="-1" />
        <remove statusCode="403" subStatusCode="-1" />      
        <remove statusCode="404" subStatusCode="-1" />                
        <remove statusCode="500" subStatusCode="-1" />
          <!-- full url when responsemode is Redirect -->
        <error statusCode="401" path="http://foo.com/default.htm" responseMode="Redirect" />
          <!-- local relative path when responsemode is ExecuteURL -->
        <error statusCode="403" path="~/404error.aspx" responseMode="ExecuteURL" />
        <error statusCode="404" path="~/404error.aspx" responseMode="ExecuteURL" />                
        <error statusCode="500" path="~/404error.aspx" responseMode="ExecuteURL" />
      </httpErrors>
      <modules runAllManagedModulesForAllRequests="true"/>
   </system.webServer>
</configuration>

显然我需要为除404页面以外的其他页面更改路径,但我只想测试这些错误。那个最后的web.config效果更糟。

请问我需要修改什么?我希望得到完整的<configuration>,因为我经常感到困惑哪些内容应该放在哪里。

编辑1

我看到很多开发者建议将其制作成HTML页面。所以现在我的页面是404.html,以下是更新后的web.config

<configuration>
  <system.web>
    <customErrors mode="On"
              redirectMode="ResponseRewrite">
        <error statusCode="404"
           redirect="~/404.html"/>
      </customErrors>
      <globalization
        fileEncoding="utf-8"
        requestEncoding="utf-8"
        responseEncoding="utf-8"
        culture="en-US"
        uiCulture="de-DE"
      />
  </system.web>

  <system.webServer>
    <httpErrors errorMode="Custom"
            defaultResponseMode="File">
      <remove statusCode="404"/>
      <error statusCode="404"
          path="~/404.html"/>
    </httpErrors>
  </system.webServer>
</configuration>

2
假设您使用的是 IIS 6 或 IIS 7,但是根据您的问题,应用程序池处于经典模式下,那么您需要在 IIS 中进行管理,具体操作请参考以下答案:https://dev59.com/x0XRa4cB1Zd3GeqPqk0t#133261 - Gary.S
1
好问题,您使用的是哪个版本的IIS?如果您使用的是IIS 7或更高版本,则可以将您的ApplicationPool(运行您的网站的那个)设置为“集成模式”,然后它应该可以工作。 - Saturn K
你是如何解决这个问题的? - Giox
@Giox 我不记得我最终选择了什么,但那是几年前的事了。我认为Keith的答案是最好的。 - JGallardo
2个回答

10

您需要配置 <httpErrors> 元素。这将为静态文件和服务器页面配置错误页面。

您第三次尝试的 "web.config (3)" 和 "Edit 1" 已经非常接近了。问题在于您不能在此处使用应用程序相对路径(例如:"~/404.html"),它们必须是相对于站点根目录的路径(例如:"/404.html")。

<?xml version="1.0"?>
<configuration>
   <system.webServer>
      <httpErrors>
        <remove statusCode="401" subStatusCode="-1" />
        <remove statusCode="403" subStatusCode="-1" />      
        <remove statusCode="404" subStatusCode="-1" />                
        <remove statusCode="500" subStatusCode="-1" />
          <!-- full url when responsemode is Redirect -->
        <error statusCode="401" path="http://foo.com/default.htm" responseMode="Redirect" />
          <!-- local relative path when responsemode is ExecuteURL -->
        <error statusCode="403" path="/404error.aspx" responseMode="ExecuteURL" />
        <error statusCode="404" path="/404error.aspx" responseMode="ExecuteURL" />                
        <error statusCode="500" path="/404error.aspx" responseMode="ExecuteURL" />
      </httpErrors>
   </system.webServer>
</configuration>

-1

在示例 customErrors 部分中的设置会导致任何未处理的 HTTP 404(文件未找到)错误被重定向到 Http404ErrorPage.aspx 文件。如果请求 .aspx 文件、.asmx 文件等,且所请求的文件不存在,则会发生这些 HTTP 404 错误。所有其他未处理的 ASP.NET 文件中的错误都将被重定向到 DefaultRedirectErrorPage.aspx 文件。

<configuration>
  <appSettings/>
  <connectionStrings/>
  <system.web>
    <compilation debug="true" />

    <!-- Turn on Custom Errors -->
    <customErrors mode="On" 
      defaultRedirect="DefaultRedirectErrorPage.aspx">
      <error statusCode="404" redirect="Http404ErrorPage.aspx"/>
    </customErrors>

  </system.web>
</configuration>

注意: 在此示例中,模式属性设置为“On”,以便在Visual Studio中运行示例时显示错误消息。在生产环境中,此设置通常为“RemoteOnly”。ASP.NET然后将错误页面呈现给外部用户。如果在服务器计算机(localhost)上发出请求,则ASP.NET会呈现一个带有详细错误信息的页面。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接