URL中的点导致ASP.NET MVC和IIS出现404

本文翻译自:Dots in URL causes 404 with ASP.NET mvc and IIS

I have a project that requires my URLs have dots in the path. 我有一个项目,要求我的URL路径中有点。 For example I may have a URL such as www.example.com/people/michael.phelps 例如,我可能有一个网址,例如www.example.com/people/michael.phelps

URLs with the dot generate a 404. My routing is fine. 带有点的URL会生成404。我的路由很好。 If I pass in michaelphelps, without the dot, then everything works. 如果我通过michaelphelps,但没有点号,则一切正常。 If I add the dot I get a 404 error. 如果添加点,则会出现404错误。 The sample site is running on Windows 7 with IIS8 Express. 该示例站点在带有IIS8 Express的Windows 7上运行。 URLScan is not running. URLScan未运行。

I tried adding the following to my web.config: 我尝试将以下内容添加到我的web.config中:

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

Unfortunately that didn't make a difference. 不幸的是,这并没有改变。 I just receive a 404.0 Not Found error. 我刚刚收到404.0找不到错误。

This is a MVC4 project but I don't think that's relevant. 这是一个MVC4项目,但我认为这无关紧要。 My routing works fine and the parameters I expect are there, until they include a dot. 我的路由工作正常,期望的参数在那里,直到它们包含一个点。

What do I need to configure so I can have dots in my URL? 我需要配置什么才能在URL中加点?


#1楼

参考:https://stackoom.com/question/nDCw/URL中的点导致ASP-NET-MVC和IIS出现


#2楼

I believe you have to set the property relaxedUrlToFileSystemMapping in your web.config. 我相信您必须在web.config中设置属性relaxedUrlToFileSystemMapping。 Haack wrote an article about this a little while ago (and there are some other SO posts asking the same types of question) Haack不久前写了一篇关于此的文章(还有其他一些SO帖子问同样的问题)

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

#3楼

After some poking around I found that relaxedUrlToFileSystemMapping did not work at all for me, what worked in my case was setting RAMMFAR to true, the same is valid for (.net 4.0 + mvc3) and (.net 4.5 + mvc4). 经过一番摸索后,我发现对我而言,relaxedUrlToFileSystemMapping根本不起作用,在我的情况下,有效的是将RAMMFAR设置为true,这对于(.net 4.0 + mvc3)和(.net 4.5 + mvc4)同样有效。

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

Be aware when setting RAMMFAR true Hanselman post about RAMMFAR and performance 在设置RAMMFAR时要特别注意Hanselman发表有关RAMMFAR和性能的文章


#4楼

I got this working by editing my site's HTTP handlers. 我通过编辑站点的HTTP处理程序来完成此工作。 For my needs this works well and resolves my issue. 对于我的需求,这很好并且可以解决我的问题。

I simply added a new HTTP handler that looks for specific path criteria. 我只是添加了一个新的HTTP处理程序来查找特定的路径条件。 If the request matches it is correctly sent to .NET for processing. 如果请求匹配,则将其正确发送到.NET进行处理。 I'm much happier with this solution that the URLRewrite hack or enabling RAMMFAR. 我对URLRewrite hack或启用RAMMFAR的解决方案非常满意。

For example to have .NET process the URL www.example.com/people/michael.phelps add the following line to your site's web.config within the system.webServer / handlers element: 例如,要让.NET处理URL www.example.com/people/michael.phelps,请将以下行添加到system.webServer / handlers元素中站点的web.config中:

<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 编辑

There are other posts suggesting that the solution to this issue is RAMMFAR or RunAllManagedModulesForAllRequests . 还有其他文章建议解决此问题的方法是RAMMFARRunAllManagedModulesForAllRequests Enabling this option will enable all managed modules for all requests. 启用此选项将为所有请求启用所有托管模块。 That means static files such as images, PDFs and everything else will be processed by .NET when they don't need to be. 这意味着静态文件(例如图像,PDF和其他所有文件)在不需要时将由.NET处理。 This options is best left off unless you have a specific case for it. 除非您有特殊情况,否则最好不要使用此选项。


#5楼

You might want to think about using dashes instead of periods. 您可能需要考虑使用破折号而不是句点。

In Pro ASP MVC 3 Framework they suggest this about making friendly URLs: Pro ASP MVC 3 Framework中,他们建议制作友好的URL:

Avoid symbols, codes, and character sequences. 避免使用符号,代码和字符序列。 If you want a word separator, use a dash (/my-great-article). 如果要使用单词分隔符,请使用破折号(/ my-great-article)。 Underscores are unfriendly, and URL-encoded spaces are bizarre (/my+great+article) or disgusting (/my%20great%20article). 下划线是不友好的,URL编码的空格是奇怪的(/ my + great + article)或令人作呕的(/ my%20great%20article)。

It also mentions that URLs should be be easy to read and change for humans. 它还提到URL应该易于阅读和更改。 Maybe a reason to think about using a dash instead of a dot also comes from the same book: 也许考虑用破折号代替点的原因也来自同一本书:

Don't use file name extensions for HTML pages (.aspx or .mvc), but do use them for specialized file types (.jpg, .pdf, .zip, etc). 不要对HTML页面(.aspx或.mvc)使用文件扩展名,而要对特殊的文件类型(.jpg,.pdf,.zip等)使用扩展名。 Web browsers don't care about file name extensions if you set the MIME type appropriately, but humans still expect PDF files to end with .pdf 如果您适当地设置MIME类型,Web浏览器将不关心文件扩展名,但是人们仍然希望PDF文件以.pdf结尾。

So while a period is still readable to humans (though less readable than dashes, IMO), it might still be a bit confusing/misleading depending on what comes after the period. 因此,尽管人类仍然可以理解某个时间段(尽管与破折号相比,IMO的可读性较低),但根据该时间段之后的情况,可能仍会有些混乱/误导。 What if someone has a last name of zip? 如果某人姓zip怎么办? Then the URL will be /John.zip instead of /John-zip, something that can be misleading even to the developer that wrote the application. 然后,该URL将是/John.zip而不是/ John-zip,即使对于编写该应用程序的开发人员,也可能会产生误导。


#6楼

Just add this section to Web.config, and all requests to the route/{*pathInfo} will be handled by the specified handler, even when there are dots in pathInfo. 只需将此部分添加到Web.config中,对path / {* pathInfo}的所有请求都将由指定的处理程序处理,即使pathInfo中有点也是如此。 (taken from ServiceStack MVC Host Web.config example and this answer https://stackoverflow.com/a/12151501/801189 ) (摘自ServiceStack MVC主机Web.config示例,此答案为https://stackoverflow.com/a/12151501/801189

This should work for both IIS 6 & 7. You could assign specific handlers to different paths after the 'route' by modifying path="*" in 'add' elements 这对IIS 6和7均适用。您可以通过修改“添加”元素中的path =“ *”,将特定处理程序分配给“路由”之后的不同路径。

  <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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值