在URL中允许使用星号

raR*_*aRa 6 .net asp.net .net-4.0 asp.net-mvc-2

我在我的网站的URL中允许使用星号(*)时遇到了麻烦.我正在运行ASP.NET MVC 2和.NET 4.0.

这是一个描述问题的例子:

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

用户名是Nice*,ASP.NET说URL中有非法字符:

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.
Run Code Online (Sandbox Code Playgroud)

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

<pages validateRequest="false">
Run Code Online (Sandbox Code Playgroud)

<httpRuntime requestPathInvalidCharacters="" requestValidationMode="2.0" />
Run Code Online (Sandbox Code Playgroud)

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

谢谢!

tur*_*ick 10

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

其他保留字符

星号("*",ASCII 2A十六进制)和感叹号("!",ASCII 21十六进制)保留用于在特定方案中具有特殊的重要性.


小智 6

星号(*)是具有特殊含义的保留字符,因此不应在URI中错误地使用它.相反,您应该在将每个用户名插入URI之前对其进行百分比编码.

在百分比编码下,星号字符变为"%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之前,始终对数据进行百分比编码可以省去很多麻烦.


raR*_*aRa 5

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

例如:

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

这样它似乎工作得很好。

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