C# 我的网址编码有什么问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/665354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's wrong with my url encoding?
提问by Boris Callens
In my asp.net mvc application I created the following link:
在我的 asp.net mvc 应用程序中,我创建了以下链接:
http://localhost:2689/en/Formula.mvc/351702++LYS+GRONN+5G+9%252f2++fds
I get error 400 (bad request).
我收到错误 400(错误的请求)。
I think it blocks at the %25 (forward slash).
What am I doing wrong?
我认为它在 %25(正斜杠)处阻塞。
我究竟做错了什么?
--EDIT 3--
I tried not encoding anything at all but rather rely on the default encoding of Url.RouteUrl().
It seems that this doesn't encode the "/" for some reason.
If I encode it myself first, I end up with the doubel encoded %252f. This gives me a bad request for some reason..
Why?!
--EDIT 3--
我尝试不编码任何东西,而是依赖于 Url.RouteUrl() 的默认编码。
由于某种原因,这似乎没有对“/”进行编码。
如果我先自己编码,我最终会得到双倍编码的 %252f。出于某种原因,这给了我一个不好的要求..为什么?!
--EDIT 2--
I generated the last part of the URI as follows:
--EDIT 2--
我生成了 URI 的最后一部分,如下所示:
- Take the id.toString
- Take the HttpUtility.UrlEncode(name)
- Take the HttpUtility.UrlEncode(code)
- String.Format("{0}--{1}--{2}") with the values from the previous parts
- Add it as a parameter to Url.RouteUrl()
- 取 id.toString
- 取 HttpUtility.UrlEncode(name)
- 以 HttpUtility.UrlEncode(code)
- String.Format("{0}--{1}--{2}") 与前面部分的值
- 将其作为参数添加到 Url.RouteUrl()
After that my action gets this parameter again, splits it at -- and HttpUtility.Decode() the values back.
之后,我的操作再次获取此参数,将其拆分为 -- 和 HttpUtility.Decode() 返回值。
I do it this way because the two last parameters are optional, but functional parameters. IF they are defined in a previous step, they have to be carried along to the other pages.
Less abstract: A color can have multiple names, but if a user selected it by a particular name, it should be kept throughout all the other pages.
我这样做是因为最后两个参数是可选的,但功能参数。如果它们是在上一步中定义的,则必须将它们带到其他页面。
不那么抽象:一种颜色可以有多个名称,但如果用户通过特定名称选择它,则应在所有其他页面中保留它。
--EDIT 1--
It also looks like HttpUtility.UrlEncode() and Url.Encode() return different results :S
--EDIT 1--
看起来 HttpUtility.UrlEncode() 和 Url.Encode() 返回不同的结果:S
If I don't encode the "/", it acts as a separator=>no luck there. If I encode it with Url.Encode() I end up with %2F => Code 400 If I encode it with HttpUtility.UrlEncode() I end up with %25 => code 400
如果我不对“/”进行编码,它将充当分隔符=> 那里没有运气。如果我用 Url.Encode() 对其进行编码,我会得到 %2F => Code 400 如果我用 HttpUtility.UrlEncode() 对其进行编码,我会得到 %25 => 代码 400
Because 400 doesn't even let it through to asp.net-mvc, the route debugger is of no use :(
因为 400 甚至不让它通过 asp.net-mvc,所以路由调试器没有用:(
采纳答案by Mathias F
I was there a couple of days ago. If you can accept unreadable route-values in the URL try this: URL-encoded slash in URL
几天前我在那里。如果您可以接受 URL 中不可读的路由值,请尝试以下操作: URL-encoded slash in URL
回答by Jonathan Parker
Have you run the Routing debugger: http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
您是否运行了路由调试器:http: //haacked.com/archive/2008/03/13/url-routing-debugger.aspx
回答by Marc Gravell
I haven't looked too much at the encoding - but note that if this is to be storedsomewhere (or acted upon in some way), then a POST would be more appropriate. If the text on the right is actually representative of the data with id 351702 (a vanity url, much like /665354/whats-wrong-with-my-url-encoding
), then you should humanize the text. Much as the spaces have been removed from the above. It is also common to have this as a separate level in the route that is simply discarded.
我没有过多研究编码 - 但请注意,如果将其存储在某处(或以某种方式进行操作),那么 POST 会更合适。如果右侧的文本实际上代表了 id 为 351702 的数据(一个虚 url,很像/665354/whats-wrong-with-my-url-encoding
),那么您应该对文本进行人性化。就像上面的空格已被删除一样。将此作为路由中的单独级别并被简单丢弃的情况也很常见。
Generally, MVC urls should be comprehensible.
一般来说,MVC url 应该是可以理解的。
回答by vartec
%25
is actually encoded "%", so %252f
is encoded "%2f".
%25
实际上编码为“%”,因此%252f
编码为“%2f”。
%2f
(encoded "/") is not allowed in URL unless you explicitly allow it in webserver's configuration.
%2f
除非您在网络服务器的配置中明确允许,否则 URL 中不允许使用(编码为“/”)。
回答by Khaja Minhajuddin
You can't use a forward slash as a value in the URL. Here is a nice post about creating browser and SEO friendly URLS => http://www.dominicpettifer.co.uk/displayBlog.aspx?id=34
您不能使用正斜杠作为 URL 中的值。这是一篇关于创建浏览器和 SEO 友好 URLS 的好帖子 => http://www.domincpettifer.co.uk/displayBlog.aspx?id=34
[Edit] Whenever you create a route you associate it with a URL pattern (The default pattern is {controller}/{action}/{id}). And in this url pattern you are supposed to use the forward slash to separate different tokens. Hope that helps
[编辑] 每当您创建路由时,您都会将其与 URL 模式相关联(默认模式是 {controller}/{action}/{id})。在这个 url 模式中,你应该使用正斜杠来分隔不同的标记。希望有帮助
回答by Jonathan Parker
W3Schools works fine: http://www.w3schools.com/TAGS/html_form_submit.asp?text=hello/world
W3Schools 工作正常:http: //www.w3schools.com/TAGS/html_form_submit.asp?text=hello/world
Here's the URL encoding reference: http://www.w3schools.com/TAGS/ref_urlencode.asp
这是 URL 编码参考:http: //www.w3schools.com/TAGS/ref_urlencode.asp