如何允许“路径中的非法字符"? [英] Howto allow "Illegal characters in path"?

查看:29
本文介绍了如何允许“路径中的非法字符"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MVC.NET 应用程序,其中一个路由如下:

I have a MVC.NET application with one route as follows:

routes.MapRoute("member", "member/{id}/{*name}", new { controller = "member", action = "Details", id = "" }, new { 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

这通常工作正常,但如果路径包含非法字符(例如双引号:http://domain/member/123/my_"user"_name) 我收到一个System.ArgumentException: Illegal characters in path."

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."

经过多次谷歌搜索后,最好的建议似乎是确保 url 不包含任何此类字符.不幸的是,在这种情况下,这是我无法控制的.

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?

推荐答案

Scott Hanselman 在 在路径中允许非法字符.

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="&lt;,&gt;,*,%,:,&amp;," />
</system.web>

您可能还需要按照 hbruce 建议:

<system.webServer>
    <security>
        <requestFiltering allowDoubleEscaping="true"/>
    </security>
</system.webServer>

仍有一些某些路径不起作用(例如/search/% ),因为您将收到 400 Bad Request - Invalid URL"消息.我发现的唯一解决方法是将该部分用作查询字符串:/search?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.

这篇关于如何允许“路径中的非法字符"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆