2

I am trying to encode searches that are sent as querystrings (Response.Redirect("/Find/" + TextBoxSearch.Text);). There is a row in the database with names including / and +, and when that enters the URL things stop working properly. I have tried encoding like this:

String encode = HttpUtility.UrlEncode(TextBoxSearch.Text);
Response.Redirect("/Find/" + encode);

But can' get it to work, what am I missing? Pretend the search value is ex/ex 18+. How could I get this to work as a querystring?

Don't know if this is important but here is how I get the querysting in my Find-page:

IList<string> segments = Request.GetFriendlyUrlSegments();
string val = "";
for (int i = 0; i < segments.Count; i++)
  {
     val = segments[i];
  }

search = val;

I can't even encode spaces properly. I try:

String encoded = Uri.EscapeDataString(TextBoxSearch.Text);
Response.Redirect("/Find/" + encoded);

But this does not turn spaces in the querystring in to %20. It does transform "/" though.

EDIT: At this point I would be happy to just turn this url localhost/Find/here are spaces in to localhost/Find/here+are+spaces

EDIT: I have been searching and trying solutions for over 5 hours now. Can anyone just tell me this:

  • If I redirect like this Response.Redirect("/Find/" + search);
  • And I make a search like this Social media
  • I then Get the queryString as the code above using segments.
  • Now I want to display info about Social media from my database
  • but at the same time I want the url to say Find/Social+media

PS: Do I need to encode every url-string? or just where I use signs and spaces.

Rodal
  • 131
  • 1
  • 1
  • 9
  • Use [Entity](http://www.w3schools.com/html/html_entities.asp) to do so, slashes are exclusive for url, so you might using slash entity equivalent `.` and + entity equivalent `+`. Your Url will look like this : `http://www.mywebsite.com/Find/ex.ex 18+`. – Anwar Mar 09 '15 at 08:20
  • But how would set this in code? how does my app know to turn these values in to Entities? I have places where you can press the value from the database instead of searching for it etc. – Rodal Mar 09 '15 at 08:23
  • Then when you want to get back the url, to know how to turn those into "normal" characters, use `HttpUtility.HtmlDecode(my_url_string)` (see more in [this Stackoverflow question](http://stackoverflow.com/questions/8348879/decoding-all-html-entities)). – Anwar Mar 09 '15 at 08:28
  • None of this is necessary, in fact treating URLs like HTML is a bad idea. `UrlEncode` isn't broken, it converts spaces to `+` which *are* valid in query strings. Use UrlPathEncode if spaces are expected in the address – Panagiotis Kanavos Mar 09 '15 at 10:15

1 Answers1

0

Instead of HttpUtility.UrlEncode use HttpUtility.UrlPathEncode. From the documentation of UrlEncode:

You can encode a URL using with the UrlEncode method or the UrlPathEncode method. However, the methods return different results. The UrlEncode method converts each space character to a plus character (+). The UrlPathEncode method converts each space character into the string "%20", which represents a space in hexadecimal notation. Use the UrlPathEncode method when you encode the path portion of a URL in order to guarantee a consistent decoded URL, regardless of which platform or browser performs the decoding.

The + character does represent a space in query strings but not in the address part.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236