What's wrong with my url encoding?

15,984

Solution 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

Solution 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.

Share:
15,984
Boris Callens
Author by

Boris Callens

Senior .net programmer. Belgium(Antwerp) based. linked-in My real email is gmail.

Updated on June 23, 2022

Comments

  • Boris Callens
    Boris Callens almost 2 years

    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
    Boris Callens about 15 years
    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
    Boris Callens about 15 years
    Also, there's no storing involved. I know about the dangers of making any data changing available through get.
  • Boris Callens
    Boris Callens about 15 years
    So what's the alternative then? And why isn't URL encode catching this?
  • vartec
    vartec about 15 years
    Yes you can. It's only the default configuration, that doesn't allow it.
  • Boris Callens
    Boris Callens about 15 years
    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.
  • Vinil
    Vinil about 15 years
    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.
  • vartec
    vartec about 15 years
    %252F is not / its double encoded slash. There is no reason why it shouldn't work.
  • Vinil
    Vinil about 15 years
    You are right, Whatever I said is right just for the "Forward Slash" character, It should work for the "%" character just fine. My bad.
  • Boris Callens
    Boris Callens about 15 years
    the query string can indeed. But this is not part of the querystring. Nor can I make it part of it.
  • Boris Callens
    Boris Callens about 15 years
    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.