问题描述
我有一个路线MVC.NET应用程序如下:
I have a MVC.NET application with one route as follows:
routes.MapRoute(会员,会员/ {ID} / {*}名,新{控制器=成员,行动=详细信息,ID =},新{ID = @\\ D +});
因此,链接可能是这样的:http://domain/member/123/any_kind_of_username
Thus, a link could be something like this: http://domain/member/123/any_kind_of_username
这工作在一般正常,但如果路径中包含非法字符(如双qoute:用户_name)我得到一个System.ArgumentException:路径中具有非法字符
This works fine in general but if the path contains illegal characters (e.g. a double qoute: http://domain/member/123/my_"user"_name) I get a "System.ArgumentException: Illegal characters in path."
很多谷歌搜索最好的建议后似乎是确保该网址不包含任何此类字符。不幸的是,我的这种情况下的控制。
After much googling the best suggestions seems to be to make sure that the url doesn't contain any such characters. Unfortunately, that is out of my control in this case.
有没有办法来解决此问题?
Is there a way to work around this?
推荐答案
斯科特汉塞尔尔曼张贴在一个很好的总结allowing在路径中非法字符。
Scott Hanselman posted a good summary on allowing illegal characters in the path.
如果你真的想允许受限制的字符,通过编辑你的web.config(这将可能只在.NET 4和IIS7工作)从列表中删除它们:
If you really want to allow characters that are restricted, remove them from the list by editing your web.config (this will probably only work in .NET 4 and on IIS7):
<system.web>
<httpRuntime requestValidationMode="2.0" relaxedUrlToFileSystemMapping="true" requestPathInvalidCharacters="<,>,*,%,:,&,\" />
</system.web>
您可能还需要做到像hbruce建议:
<system.webServer>
<security>
<requestFiltering allowDoubleEscaping="true"/>
</security>
</system.webServer>
还有一些特定的路径,这是行不通的,(如/搜索/%),你会得到400错误的请求 - 无效的URL消息。我发现的唯一的解决方法是使用一部分作为查询字符串:?/搜Q =%以上步骤
There are still some certain paths that will not work, (such as /search/% ) as you will get the 400 "Bad Request - Invalid URL" message. The only workaround I've found is to use that part as a querystring: /search?q=% with the above steps.
这篇关于HOWTO允许&QUOT;路径&QUOT非法字符;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!