views:

2817

answers:

6

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

A: 

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

Jonathan Parker
I Will update the OP about that.
borisCallens
A: 

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
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.
borisCallens
Also, there's no storing involved. I know about the dangers of making any data changing available through get.
borisCallens
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.
borisCallens
A: 

%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
A: 

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
So what's the alternative then? And why isn't URL encode catching this?
borisCallens
Yes you can. It's only the default configuration, that doesn't allow it.
vartec
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
%252F is not / its *double* encoded slash. There is no reason why it shouldn't work.
vartec
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
Indeed, it should. But it doesn't...
borisCallens
A: 

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
the query string can indeed. But this is not part of the querystring. Nor can I make it part of it.
borisCallens
+1  A: 

I was there a couple of days ago. If you can accept unreadable route-values in the URL try this: http://stackoverflow.com/questions/591694/url-encoded-slash-in-url

Malcolm Frexner
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.
borisCallens