4

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 :(

Boris Callens
  • 90,659
  • 85
  • 207
  • 305

6 Answers6

3

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

Community
  • 1
  • 1
Mathias F
  • 15,906
  • 22
  • 89
  • 159
  • 1
    I ended up doing something similar, only I exchanged the unescapable values to words. E.g.: "-slash-". Not what I call perfect, but it's the best I can come up with for now. – Boris Callens Mar 23 '09 at 08:44
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.

vartec
  • 131,205
  • 36
  • 218
  • 244
0

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

Jonathan Parker
  • 6,705
  • 3
  • 43
  • 54
0

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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • This part of the url defines first the id of a color. Then the two parts after that are optional to define the prefered name and code. It has to be sent throughout the application. I am aiming to be RESTfull, not at making the URL a human interface. Which is a silly idea in my opinion. – Boris Callens Mar 20 '09 at 08:51
  • Also, there's no storing involved. I know about the dangers of making any data changing available through get. – Boris Callens Mar 20 '09 at 08:52
  • I now get what you mean with keeping the ID appart from the description. In my case the description isn't just for SEO. It has a functional purpose. Because of the way my source database works (I can't change it) the description is the key of something. – Boris Callens Mar 20 '09 at 09:11
0

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

Khaja Minhajuddin
  • 6,653
  • 7
  • 45
  • 47
  • So what's the alternative then? And why isn't URL encode catching this? – Boris Callens Mar 20 '09 at 08:59
  • Yes you can. It's only the default configuration, that doesn't allow it. – vartec Mar 20 '09 at 09:09
  • 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. – Khaja Minhajuddin Mar 20 '09 at 09:12
  • %252F is not / its *double* encoded slash. There is no reason why it shouldn't work. – vartec Mar 20 '09 at 09:16
  • You are right, Whatever I said is right just for the "Forward Slash" character, It should work for the "%" character just fine. My bad. – Khaja Minhajuddin Mar 20 '09 at 09:22
0

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

Jonathan Parker
  • 6,705
  • 3
  • 43
  • 54