Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL-encoded slash in URL

My Map is:

routes.MapRoute(    "Default",                                             // Route name    "{controller}/{action}/{id}",                          // URL with params    new { controller = "Home", action = "Index", id = "" } // Param defaults ); 

If I use the URL http://localhost:5000/Home/About/100%2f200 there is no matching route. I change the URL to http://localhost:5000/Home/About/100 then the route is matched again.

Is there any easy way to work with parameters that contain slashes? Other escaped values (space %20) seem to work.

EDIT:

To encode Base64 works for me. It makes the URL ugly, but that's OK for now.

public class UrlEncoder {      public string URLDecode(string  decode)     {         if (decode == null) return null;         if (decode.StartsWith("="))         {             return FromBase64(decode.TrimStart('='));         }         else         {             return HttpUtility.UrlDecode( decode) ;         }     }      public string UrlEncode(string encode)     {         if (encode == null) return null;         string encoded = HttpUtility.PathEncode(encode);         if (encoded.Replace("%20", "") == encode.Replace(" ", ""))         {             return encoded;         }         else         {             return "=" + ToBase64(encode);         }     }      public string ToBase64(string encode)     {         Byte[] btByteArray = null;         UTF8Encoding encoding = new UTF8Encoding();         btByteArray = encoding.GetBytes(encode);         string sResult = System.Convert.ToBase64String(btByteArray, 0, btByteArray.Length);         sResult = sResult.Replace("+", "-").Replace("/", "_");         return sResult;     }      public string FromBase64(string decode)     {         decode = decode.Replace("-", "+").Replace("_", "/");         UTF8Encoding encoding = new UTF8Encoding();         return encoding.GetString(Convert.FromBase64String(decode));     } } 

EDIT1:

At the end it turned out that the best way was to save a nicely formated string for each item I need to select. Thats much better because now I only encode values and never decode them. All special characters become "-". A lot of my db-tables now have this additional column "URL". The data is pretty stable, thats why I can go this way. I can even check, if the data in "URL" is unique.

EDIT2:

Also watch out for space character. It looks ok on VS integrated webserver but is different on iis7 Properly url encode space character

like image 619
Mathias F Avatar asked Feb 26 '09 17:02

Mathias F


People also ask

How do you put a slash in a URL?

A trailing slash is a forward slash (“/”) placed at the end of a URL such as domain.com/ or domain.com/page/. The trailing slash is generally used to distinguish a directory which has the trailing slash from a file that does not have the trailing slash.

Is %2F a slash?

Encoded forward slash (%2F) in parameter not routing correctly #22125.

What do the slashes mean in a URL?

The addition of a slash at the end of a URL instructs the web server to search for a directory. This speeds the web page loading because the server will retrieve the content of the web page without wasting time searching for the file.


2 Answers

If it's only your last parameter, you could do:

routes.MapRoute(     "Default",                                                // Route name     "{controller}/{action}/{*id}",                            // URL with parameters     new { controller = "Home", action = "Index", id = "" });  // Parameter defaults 
like image 74
mmx Avatar answered Sep 24 '22 11:09

mmx


Here's a simple explanation of the solution and a summation of what has already been said.

Request side:

  1. UrlEncode your path.
  2. Replace the '%' with '!'.
  3. Make the request.

Response side:

  1. Replace the '!' with '%'.
  2. UrlDecode your path.
  3. Use the parameters as they were intended.

Rinse, repeat, enjoy.

like image 20
Don Rolling Avatar answered Sep 25 '22 11:09

Don Rolling