Well, of course you can't put a space in a URI in any case. What you can do is put %20
in which is the way to encode a space at any point in a URI as well as +
being the normal way to encode a space in the query portion, and the recommended way to do so in that portion of it because %20 can cause problems.
Because of this is an implementaiton detail application/x-www-form-urlencoded and we normally care about the actual data sent, Request.QueryString[]
does the unescaping for you, turning both +
and %20
into a space.
You want to look at the Request.RawUrl
(returns a string) or Request.Url
which returns a Uri
. Probably the easiest is to feed Request.Url
into a UriBuilder
so you can change just the query and get a Uri
back.
Still, I'd recommend the opposite approach, if you're having issues with duplicate content due to the two possible ways of encoding a space in the query string, go with the recommended norm and turn the cases of %20
into +
rather than the other way around.
var u = Request.Url;
if(u.Query.Contains("%20"))
{
var ub = new UriBuilder(u);
Console.WriteLine(ub.Query);
string query = ub.Query;
//note bug in Query property - it includes ? in get and expects it not to be there on set
ub.Query = ub.Query.Replace("%20", "+").Substring(1);
Response.StatusCode = 301;
Response.RedirectLocation = ub.Uri.AbsoluteUri;
Response.End();
}