如何将包含冒号的 GET 请求发送到 ASP.NET MVC2 控制器?
这工作正常:
GET /mvc/Movies/TitleIncludes/Lara%20Croft
当我提交包含冒号的请求时,如下所示:
GET /mvc/Movies/TitleIncludes/Lara%20Croft:%20Tomb
...它会生成 400 错误。该错误表示 ASP.NET 在 URL 中检测到无效字符。
如果我尝试进行 url 转义,请求将如下所示:
GET /mvc/Movies/TitleIncludes/Lara%20Croft%3A%20Tomb
...这也会给我一个 400 错误。
如果我用 | 替换冒号:
GET /mvc/Movies/TitleIncludes/Lara%20Croft|%20Tomb
..这也被视为非法而被拒绝,这次有 500 错误。消息:路径中存在非法字符。
URL 转义 |导致同样的错误。
我真的真的不想使用查询字符串参数。
This works fine:
GET /mvc/Movies/TitleIncludes/Lara%20Croft
When I submit a request that contains a colon, like this:
GET /mvc/Movies/TitleIncludes/Lara%20Croft:%20Tomb
...it generates a 400 error. The error says ASP.NET detected invalid characters in the URL.
If I try url-escaping, the request looks like this:
GET /mvc/Movies/TitleIncludes/Lara%20Croft%3A%20Tomb
...and this also gives me a 400 error.
If I replace the colon with a | :
GET /mvc/Movies/TitleIncludes/Lara%20Croft|%20Tomb
..that was also rejeted as illegal, this time with a 500 error. The message: Illegal characters in path.
URL-escaping that | results in the same error.
I really, really don't want to use a querystring parameter.
related:
Sending URLs/paths to ASP.NET MVC controller actions
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现URL编码不起作用,但是自定义编码起作用。
我猜想 ASPNET MVC 使用文件系统来进行解析和路由,因为 URL 中的字符在文件系统中不合法,会导致 500 或 400 错误。
所以我所做的就是用 unicode 替换冒号
¡ 在 javascript 端输入字符,然后在操作中执行相反的操作。像这样:
browser:
在操作中,在使用参数之前调用此转换:
相同的方法适用于斜杠 - 只需选择不同的 unicode 字符。当然,如果您的字符串参数本身是 unicode,那么您必须对“编码”形式使用不可打印的字符。
I found that URL encoding did not work, but custom encoding did.
I guess ASPNET MVC uses the filesystem to do the parsing and routing, because a character in the URL that is not legal in the filesystem, causes a 500 or 400 error.
So what I did was replace colons with the unicode
¡ character in the javascript side, and then do the converse in the action. like this:
browser:
in the action, call this conversion before using the argument:
The same approach works for slashes - just pick a different unicode character. Of course if your string arguments themselves are unicode, then you'll have to use non-printable characters for the "encoded" forms.
如果 SEO 不是问题,您可以使用 base64,然后对其进行 urlencode。第一步之后,您将拥有的每个字符都将被轻松编码。 .NET 中的解码与使用 System.Web.HttpUtility 和 System.Convert 中的帮助程序一样简单。
If SEO is not a problem you may use base64 and then urlencode that. After the first step every character you'll have will be easily encoded. Decoding in .NET is as easy as using the helper in System.Web.HttpUtility and System.Convert.
类似的回答在这里:
https://stackoverflow.com/a/12037000/134761
使用问号和与号作为参数,并对参数进行 URL 编码。
示例: GET /mvc/Movies/TitleInincludes?title=Lara%20Croft%3A%20Tomb
我同意将内容编码到 url 中也很好,但可能有一个很好的理由不这样做。
Similar answered here:
https://stackoverflow.com/a/12037000/134761
Use question mark and ampersands for arguments and URL encode the arguments.
Example: GET /mvc/Movies/TitleIncludes?title=Lara%20Croft%3A%20Tomb
I agree it would be nice to encode things into the url as well, but there is probably a good reason not to.