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.