What's wrong with my url encoding?

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

In my asp.net mvc application I created the following link:

http://localhost:2689/en/Formula.mvc/351702++LYS+GRONN+5G+9%252f2++fds  

I get error 400 (bad request).

I think it blocks at the %25 (forward slash).
What am I doing wrong?

--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 2--
I generated the last part of the URI as follows:

  1. Take the id.toString
  2. Take the HttpUtility.UrlEncode(name)
  3. Take the HttpUtility.UrlEncode(code)
  4. String.Format("{0}--{1}--{2}") with the values from the previous parts
  5. Add it as a parameter to Url.RouteUrl()

After that my action gets this parameter again, splits it at -- and HttpUtility.Decode() the values back.

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

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

Because 400 doesn't even let it through to asp.net-mvc, the route debugger is of no use :(

回答1:

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



回答2:

%25 is actually encoded "%", so %252f is encoded "%2f".

%2f (encoded "/") is not allowed in URL unless you explicitly allow it in webserver's configuration.



回答3:

Have you run the Routing debugger: http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx



回答4:

I haven't looked too much at the encoding - but note that if this is to be stored somewhere (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.

Generally, MVC urls should be comprehensible.



回答5:

W3Schools works fine: 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



回答6:

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

[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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!