在URL中允许使用星号

时间:2010-12-31 00:03:38

标签: .net asp.net asp.net-mvc-2 .net-4.0

我在网站的网址中允许使用星号(*)时遇到问题。我正在运行ASP.NET MVC 2和.NET 4.0。

以下是描述问题的示例:

http://mysite.com/profile/view/Nice *

用户名为Nice *,ASP.NET表示网址中存在非法字符:

Illegal characters in path.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: Illegal characters in path.

我已经尝试过我在网上看到的所有Web.config方法,例如:

<pages validateRequest="false">

<httpRuntime requestPathInvalidCharacters="" requestValidationMode="2.0" />

所以我的问题是:是否可以在URL中允许使用星号?如果没有,.NET中是否有一些编码方法可以编码星号(*)?

谢谢!

3 个答案:

答案 0 :(得分:10)

http://www.w3.org/Addressing/URL/4_URI_Recommentations.html

  

其他保留字符

     

星号(“*”,ASCII 2A hex)和   感叹号(“!”,ASCII 21十六进制)   保留用作特殊的   具体方案中的重要性。

答案 1 :(得分:6)

星号(*)是具有特殊含义的保留字符,因此不应在URI中错误使用。相反,在将每个用户名插入URI之前,您应该percent encode

在百分比编码下,星号字符变为“%2A”。

所以完整,正确的URI将是: http://example.com/profile/view/Nice%2A

百分比编码用户名应自动转换回原始用户名字符串。

当您的用户将这些地址复制并粘贴到他们的邮件程序中时,这不仅允许您的URI验证服务器端,还允许验证客户端。

例如,Stack Overflow会自动超链接安全的百分比编码URI: http:// example.com/profile/view/Nice%2A

但它没有完全超链接不安全的版本: http:// example.com/profile/?user=username *

星号溢出链接中不包含星号 - 因此您的用户可能会找到错误的页面。

(对不起,我无法证明这一点 - 我只允许在我的帖子中包含两个链接。)

在将数据插入URI之前,始终对数据进行百分比编码,可以省去很多麻烦。

答案 2 :(得分:2)

我的解决方案是将其更改为查询字符串。

E.g:

http://mysite.com/profile/?user=username *

这样看起来效果很好。

我知道星号(*)是一个保留字符,因此我甚至不应该允许用户名拥有它。但这解决了我的问题。