2

I am trying to perform the following task in asp.net

Response.Redirect("/?confim&name=John%20Smith");

When this code is executed the url I see in the browser address is "www.mysite.com/?confirm&name=John+Smith".

But what I want is "www.mysite.com/?confirm&name=John%20Smith".

I know that %20 and + are in place of a space and I know it would probably be better to have the name something like "John_Smith". But I am integrating with a third party piece of software etc, with old data and for legacy reasons it cant change.

I have tried this in another vanilla site and I don't receive the problem, so I cant help but wonder if its a global setting or something that I am missing or haven't heard of. Any help would be greatly appreciated

Thanks

Will

wdhough
  • 155
  • 3
  • 12
  • What do you see in the page source - a `%20` or a `+`? – Oded Mar 15 '12 at 19:30
  • Page source? sorry you've lost me there, its the browser address, are you saying whats the raw url once the redirect hits the server? – wdhough Mar 15 '12 at 19:38

1 Answers1

1

I don't think I understand what about the encoding you don't like---

The + is a reserved character. So even if you replace spaces with +, the + will be replaced with a % code. And " " should be replaced with %20 anyhow, and if it isn't you can use the HttpServerUtility.UrlEncode method. And if that doesn't already work, then try this hack:

string someUrlEncodedMyWay = "www.mysite.com/?confirm&name=John%20Smith";
Response.Write("<script type=\"text/javascript\">");

Response.Write("window.location = \"" + someUrlEncodedMyWay + "\"");

Response.Write("</script>");

And that will allow you to tell the browser to use a funky URL, but if the browser doesn't want to, you might end up with a different encoding of the spaces and special characters.

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
  • I have no personal issue with it, but the third party product that looks at urls needs to use %20 instead of + – wdhough Mar 15 '12 at 19:45
  • Another possible solution: response.addheader("Location",server.url encode("string")) which I got from this question: http://stackoverflow.com/questions/4765453/asp-response-redirect-does-not-urlencode-sign – MatthewMartin Mar 15 '12 at 20:01