【问题标题】:Paths ending with dots not being captured by runAllManagedModulesForAllRequests="true"以点结尾的路径未被 runAllManagedModulesForAllRequests="true" 捕获
【发布时间】:2016-11-16 11:29:01
【问题描述】:

我见过很多类似的问题,但它们都集中在我已经完成的这项任务的第一部分。如果我错过了回答我的问题的人,请告诉我。

我有一个运行 ASP .NET MVC5 站点的 IIS 7.5 服务器。作为我添加的路线的一部分:

routes.MapRoute(
    name: "Default",
    url:"{*url}",
    defaults: new { controller = "Home", action = "NotFound" }
);

这条路线充当我的 404 操作。

在我的 web.config 中,我还有:

<modules runAllManagedModulesForAllRequests="true">

结果是我现在将我的 404 通常定义为 MVC 操作,但它会响应所有 URL,包括以下内容:

/error
/err.or
/e/r/r/o/r

但是今天我注意到如果我尝试这条路线它会失败:

/error.

也就是说,任何以句点或点结尾的路线都给了我典型的:

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /error.

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34280

我在另一个项目中有类似的设置,它适用于以点结尾的 URL,但我不知道这两个项目之间有什么不同。有什么想法我可能做错了吗?或者是什么捕获了这条路线?或者是什么排除了这条路由通过 ASP .Net 处理?

【问题讨论】:

    标签: asp.net asp.net-mvc iis iis-7.5


    【解决方案1】:

    很可能,罪魁祸首是您没有启用该设置:

    <system.web>
      <httpRuntime relaxedUrlToFileSystemMapping="true" />
    </system.web>
    

    注意:根据this page,如果. 位于查询字符串的末尾,则无需启用runAllManagedModulesForAllRequests(这会影响性能)。

    另类

    如果这不起作用,有效回避问题的一种方法是简单地使用URL Rewrite Module 将路径以点结尾的任何URL 重写为/Home/NotFound。这样就无需先响应 302,然后再响应 404(这对 SEO 不利)。

    <configuration>
      <system.webServer>
        <rewrite>
          <rules>
            <rule name="Rewrite . at end of URL path to /Home/NotFound" stopProcessing="true">
              <match url="^.*\.(?:/?\?.*)?$" />
              <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
              </conditions>
              <action type="Rewrite" url="/Home/NotFound" />
            </rule>
          </rules>
        </rewrite>
      </system.webServer>
    </configuration>
    

    这假定您响应 /Home/NotFound 的操作方法正确地将 HTTP 状态代码设置为 404。

    参考资料:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-18
      • 2012-09-11
      • 2014-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多