我有一个项目,要求我的url在路径上有圆点。例如,我可能有一个URL,例如www.example.com/people/michael.phelps

带有点的url会生成404。我的路由是好的。如果我传入michaelphelps,没有点,那么一切正常。如果我加一个点,就会得到404错误。示例站点运行在Windows 7和IIS8 Express上。URLScan未运行。

我尝试在我的web.config中添加以下内容:

<security>
  <requestFiltering allowDoubleEscaping="true"/>
</security>

不幸的是,这并没有什么不同。我刚刚收到一个404.0未找到错误。

这是一个MVC4项目,但我不认为这是相关的。我的路由工作得很好,我所期望的参数都在那里,直到它们包含一个点。

我需要配置什么才能在我的URL中有圆点?


当前回答

我通过编辑我的站点的HTTP处理程序得到了这个工作。对于我的需要,这很好地解决了我的问题。

我只是添加了一个新的HTTP处理程序,用于查找特定的路径条件。如果请求匹配,则正确地将其发送到. net进行处理。我更喜欢这个解决方案,URLRewrite黑客或启用RAMMFAR。

例如,要让. net处理URL www.example.com/people/michael.phelps,请在站点的web中添加以下行。系统内配置。webServer / handlers元素:

<add name="ApiURIs-ISAPI-Integrated-4.0"
     path="/people/*"
     verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS"
     type="System.Web.Handlers.TransferRequestHandler"
     preCondition="integratedMode,runtimeVersionv4.0" />

Edit

还有其他帖子建议这个问题的解决方案是RAMMFAR或RunAllManagedModulesForAllRequests。启用此选项将为所有请求启用所有托管模块。这意味着静态文件,如图像、pdf文件和其他任何文件将在不需要时由. net处理。除非你有特定的情况,否则最好不要使用这个选项。

其他回答

我相信你必须在你的web.config中设置弛豫urltofilesystemmapping属性。不久前,Haack写了一篇关于这个问题的文章(还有其他一些SO帖子也问了同样类型的问题)

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

编辑 从下面的评论中可以看出,. net / IIS的后续版本可能需要在系统中安装这个。网络服务器的元素。

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

添加URL重写规则到Web。配置档案。您需要在IIS中已经安装URL重写模块。使用下面的重写规则作为你自己的灵感。

<?xml version="1.0" encoding="utf-8"?>
<configuration>

<system.webServer>
  <rewrite>
    <rules>
      <rule name="Add trailing slash for some URLs" stopProcessing="true">
        <match url="^(.*(\.).+[^\/])$" />
          <conditions>
              <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
              <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Redirect" url="{R:1}/" />
      </rule>
    </rules>
    </rewrite>
</system.webServer>

</configuration> 

在一些闲逛后,我发现relaxedUrlToFileSystemMapping根本不为我工作,在我的情况下工作的是将RAMMFAR设置为true,同样适用于(.net 4.0 + mvc3)和(.net 4.5 + mvc4)。

<system.webserver>
    <modules runAllManagedModulesForAllRequests="true">

当设置RAMMFAR为true时,Hanselman发布了关于RAMMFAR和性能的帖子

对于那些只在一个网页上有这个的人来说,超级简单的答案。编辑actionlink并在它的末尾加上+“/”。

  @Html.ActionLink("Edit", "Edit", new { id = item.name + "/" }) |

只需将此部分添加到Web。所有对路由/{*pathInfo}的请求都将由指定的处理程序处理,即使在pathInfo中有点。(取自ServiceStack MVC主机Web。配置示例和这个答案https://stackoverflow.com/a/12151501/801189)

这应该适用于iis6和iis7。你可以通过修改'add'元素中的path="*"为'route'后面的不同路径分配特定的处理程序

  <location path="route">
    <system.web>
      <httpHandlers>
        <add path="*" type="System.Web.Handlers.TransferRequestHandler" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" />
      </httpHandlers>
    </system.web>
    <!-- Required for IIS 7.0 -->
    <system.webServer>
      <modules runAllManagedModulesForAllRequests="true" />
      <validation validateIntegratedModeConfiguration="false" />
      <handlers>
        <add name="ApiURIs-ISAPI-Integrated-4.0" path="*" type="System.Web.Handlers.TransferRequestHandler" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" preCondition="integratedMode,runtimeVersionv4.0" />
      </handlers>
    </system.webServer>
  </location>