asp.net-mvc URL 中的点导致 404 与 ASP.NET mvc 和 IIS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11728846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dots in URL causes 404 with ASP.NET mvc and IIS
提问by Mark
I have a project that requires my URLs have dots in the path. For example I may have a URL such as www.example.com/people/michael.phelps
我有一个项目要求我的 URL 在路径中包含点。例如,我可能有一个 URL,例如 www.example.com/people/michael.phelps
URLs with the dot generate a 404. My routing is fine. If I pass in michaelphelps, without the dot, then everything works. If I add the dot I get a 404 error. The sample site is running on Windows 7 with IIS8 Express. URLScan is not running.
带点的 URL 生成 404。我的路由很好。如果我传入 michaelphhelps,没有点,那么一切正常。如果我添加点,我会收到 404 错误。示例站点在带有 IIS8 Express 的 Windows 7 上运行。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 Not Found 错误。
This is a MVC4 project but I don't think that's relevant. My routing works fine and the parameters I expect are there, until they include a dot.
这是一个 MVC4 项目,但我认为这无关紧要。我的路由工作正常,我期望的参数在那里,直到它们包含一个点。
What do I need to configure so I can have dots in my URL?
我需要配置什么才能在我的 URL 中包含点?
回答by Mark
I got this working by editing my site's HTTP handlers. For my needs this works well and resolves my issue.
我通过编辑我网站的 HTTP 处理程序来解决这个问题。对于我的需要,这很有效并解决了我的问题。
I simply added a new HTTP handler that looks for specific path criteria. If the request matches it is correctly sent to .NET for processing. I'm much happier with this solution that the URLRewrite hack or enabling RAMMFAR.
我只是添加了一个新的 HTTP 处理程序来查找特定的路径标准。如果请求匹配,它会正确发送到 .NET 进行处理。我对 URLRewrite hack 或启用 RAMMFAR 的解决方案更满意。
For example to have .NET process the URL www.example.com/people/michael.phelpsadd the following line to your site's web.config within the system.webServer / handlerselement:
例如,要让 .NET 处理 URL www.example.com/people/michael.phelps,请将以下行添加到您站点的 web.configsystem.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
编辑
There are other posts suggesting that the solution to this issue is RAMMFARor RunAllManagedModulesForAllRequests. 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. This options is best left off unless you have a specific case for it.
还有其他帖子表明此问题的解决方案是RAMMFAR或RunAllManagedModulesForAllRequests。启用此选项将为所有请求启用所有托管模块。这意味着静态文件(如图像、PDF 和其他所有内容)将在不需要时由 .NET 处理。除非您有特定情况,否则最好不要使用此选项。
回答by Tadeu Maia
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 设置为 true 时请注意Hanselman 关于 RAMMFAR 和性能的帖子
回答by Tommy
I believe you have to set the property relaxedUrlToFileSystemMapping in your web.config. Haackwrote an article about this a little while ago (and there are some other SO postsasking the same types of question)
我相信你必须在你的 web.config 中设置relaxedUrlToFileSystemMapping 属性。 Haack不久前写了一篇关于此的文章(还有其他一些SO 帖子提出了相同类型的问题)
<system.web>
<httpRuntime relaxedUrlToFileSystemMapping="true" />
EditFrom the comments below, later versions of .NET / IIS may require this to be in the system.WebServerelement.
编辑从下面的评论来看,更高版本的 .NET / IIS 可能要求在system.WebServer元素中包含它。
<system.webServer>
<httpRuntime relaxedUrlToFileSystemMapping="true" />
回答by Leon van Wyk
I got stuck on this issue for a long time following all the different remedies without avail.
在尝试了所有不同的补救措施后,我在这个问题上卡住了很长时间,但都无济于事。
I noticed that when adding a forward slash [/] to the end of the URL containing the dots [.], it did not throw a 404 error and it actually worked.
我注意到当在包含点 [.] 的 URL 末尾添加正斜杠 [/] 时,它没有抛出 404 错误并且它确实有效。
I finally solved the issue using a URL rewriter like IIS URL Rewrite to watch for a particular pattern and append the training slash.
我最终使用 URL 重写器(如 IIS URL Rewrite)解决了该问题,以观察特定模式并附加训练斜杠。
My URL looks like this: /Contact/~firstname.lastname so my pattern is simply: /Contact/~(.*[^/])$
我的 URL 看起来像这样:/Contact/~firstname.lastname 所以我的模式很简单:/Contact/~(.*[^/])$
I got this idea from Scott Forsyth, see link below: http://weblogs.asp.net/owscott/handing-mvc-paths-with-dots-in-the-path
我从 Scott Forsyth 那里得到了这个想法,请参见下面的链接:http: //weblogs.asp.net/owscott/handing-mvc-paths-with-dots-in-the-path
回答by V.B.
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. (taken from ServiceStack MVC Host Web.config example and this answer https://stackoverflow.com/a/12151501/801189)
只需将此部分添加到 Web.config 中,所有对 route/{*pathInfo} 的请求都将由指定的处理程序处理,即使 pathInfo 中有点。(取自 ServiceStack MVC Host 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。您可以通过修改“添加”元素中的“路径”=“*”,将特定处理程序分配给“路由”之后的不同路径
<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>
回答by Luke
MVC 5.0 Workaround.
MVC 5.0 解决方法。
Many of the suggested answers doesn't seem to work in MVC 5.0.
许多建议的答案似乎在 MVC 5.0 中不起作用。
As the 404 dot problem in the last section can be solved by closing that section with a trailing slash, here's the little trick I use, clean and simple.
由于最后一节中的 404 点问题可以通过用斜线结束该节来解决,这是我使用的小技巧,干净简单。
While keeping a convenient placeholder in your view:
同时在您的视图中保留一个方便的占位符:
@Html.ActionLink("Change your Town", "Manage", "GeoData", new { id = User.Identity.Name }, null)
add a little jquery/javascript to get the job done:
添加一点 jquery/javascript 来完成工作:
<script>
$('a:contains("Change your Town")').on("click", function (event) {
event.preventDefault();
window.location.href = '@Url.Action("Manage", "GeoData", new { id = User.Identity.Name })' + "/";
});</script>
please note the trailing slash, that is responsible for changing
请注意尾部斜杠,它负责改变
http://localhost:51003/GeoData/Manage/[email protected]
into
进入
http://localhost:51003/GeoData/Manage/[email protected]/
回答by Jeremy Hobbs
Super easy answer for those that only have this on one webpage. Edit your actionlink and a + "/" on the end of it.
对于那些只在一个网页上有这个的人来说,超级简单的答案。编辑您的操作链接并在其末尾添加一个 +“/”。
@Html.ActionLink("Edit", "Edit", new { id = item.name + "/" }) |
回答by GR7
Depending on how important it is for you to keep your URI without querystrings, you can also just pass the value with dots as part of the querystring, not the URI.
根据保持 URI 不带查询字符串的重要性,您也可以只将带点的值作为查询字符串的一部分传递,而不是 URI。
E.g. www.example.com/people?name=michael.phelps will work, without having to change any settings or anything.
例如 www.example.com/people?name=michael.phelps 就可以工作,无需更改任何设置或任何内容。
You lose the elegance of having a clean URI, but this solution does not require changing or adding any settings or handlers.
您失去了拥有干净 URI 的优雅,但此解决方案不需要更改或添加任何设置或处理程序。
回答by sdm350
You might want to think about using dashes instead of periods.
您可能需要考虑使用破折号而不是句点。
In Pro ASP MVC 3 Frameworkthey 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). Underscores are unfriendly, and URL-encoded spaces are bizarre (/my+great+article) or disgusting (/my%20great%20article).
避免使用符号、代码和字符序列。如果您需要单词分隔符,请使用破折号 (/my-great-article)。下划线是不友好的,URL 编码的空格是奇怪的 (/my+great+article) 或恶心的 (/my%20great%20article)。
It also mentions that URLs should be be easy to read and change for humans. Maybe a reason to think about using a dash instead of a dot also comes from the same book:
它还提到 URL 应该易于人类阅读和更改。也许考虑使用破折号而不是点的原因也来自同一本书:
Don't use file name extensions for HTML pages (.aspx or .mvc), but do use them for specialized file types (.jpg, .pdf, .zip, etc). 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
不要对 HTML 页面使用文件扩展名(.aspx 或 .mvc),但要对特殊文件类型(.jpg、.pdf、.zip 等)使用它们。如果您适当地设置了 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. What if someone has a last name of 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.
因此,虽然句号对人类来说仍然可读(虽然比破折号可读性差,IMO),但根据句号之后的内容,它可能仍然有点混乱/误导。如果有人姓 zip 怎么办?然后 URL 将是 /John.zip 而不是 /John-zip,这甚至可能误导编写应用程序的开发人员。
回答by jonduncan05
Would it be possible to change your URL structure?
For what I was working on I tried a route for
是否可以更改您的 URL 结构?
对于我正在做的事情,我尝试了一条路线
url: "Download/{fileName}"
but it failed with anything that had a . in it.
但它失败了任何带有 . 在里面。
I switched the route to
我把路线切换到
routes.MapRoute(
name: "Download",
url: "{fileName}/Download",
defaults: new { controller = "Home", action = "Download", }
);
Now I can put in localhost:xxxxx/File1.doc/Downloadand it works fine.
现在我可以放入localhost:xxxxx/File1.doc/Download并且它工作正常。
My helpers in the view also picked up on it
我在视图中的助手也注意到了它
@Html.ActionLink("click here", "Download", new { fileName = "File1.doc"})
that makes a link to the localhost:xxxxx/File1.doc/Downloadformat as well.
这也可以链接到localhost:xxxxx/File1.doc/Download格式。
Maybe you could put an unneeded word like "/view" or action on the end of your route so your property can end with a trailing /something like /mike.smith/view
也许你可以把不需要的字,如“/图”或行动路线的结束使你的财产能与后端/类似/mike.smith/view

