2

I am stumped on this scenario. Basically I have an URL that is passed to a aspx page and then I try to get query string from the URL, but what happens is when I try to get the query string from the URL it omits the '+' and replaces it with an whitespace.

My URL =  http://localhost:3872/Test.aspx?mt=jan1TNIixxA1+8tl/0vLLg2PPGq0vMOLEhFQNuG4AJU12VMZpnWTrgar82K5UlXatQT9E9EAUet+q7rq7FoTJf+S2JnSbIptgJDY1EZwRPJDTROktfu5zy25oydmSHB6a4oZetV5mI3s+0R7vW8I0S9d765RHdYU2xkRuojHYZU=

Request["mt"] =jan1TNIixxA1 8tl/0vLLg2PPGq0vMOLEhFQNuG4AJU12VMZpnWTrgar82K5UlXatQT9E9EAUet q7rq7FoTJf S2JnSbIptgJDY1EZwRPJDTROktfu5zy25oydmSHB6a4oZetV5mI3s 0R7vW8I0S9d765RHdYU2xkRuojHYZU=

As you can see these two strings are different.

I thought it was the object to string conversion but this does not seem to be the case cause the value of the object has the omitted '+' before conversion.

What can be done to avoid this character replacement (I want to try and avoid string manipulation)

Also what could the cause of this be?

SpaceApple
  • 1,309
  • 1
  • 24
  • 46

6 Answers6

1

You can use

MyUrl = MyUrl.Replace("+",@"%2B");

The problem is the '+' character is being converted to whitespace if you use httprequest. If you convert it to its hex value, you can pass it with no problem.

Kamil T
  • 2,232
  • 1
  • 19
  • 26
1

You should use HttpUtility.UrlEncode to generate you parameter value. Currently it seems you are using base64 encoding which is not optimal for query parameters.

Nenad
  • 24,809
  • 11
  • 75
  • 93
1

Use this:

mt=encodeURIComponent(mt);//if mt be --> jan1TNIixxA1+8tl/0vLLg2PPGq0vMOLEhFQNuG4AJU12VMZpnWTrgar82K5UlXatQT9E9EAUet+q7rq7FoTJf+S2JnSbIptgJDY1EZwRPJDTROktfu5zy25oydmSHB6a4oZetV5mI3s+0R7vW8I0S9d765RHdYU2xkRuojHYZU=
Response.Redirect("Test.aspx?"+mt);

this will encode your URL and after this '+' will converted to "%2B" and if you want to read encoded URL it will not converted to space.

from here

Community
  • 1
  • 1
Majid
  • 13,853
  • 15
  • 77
  • 113
1

you are getting that because + is the url encoded representation of space " ". If you want to preseve the plus sign in your value you will need to url encode it:

Send that querystring in URL encoded formate and then you will get the expected result.

see: why Request.QueryString replace + with empty char in some cases?

Community
  • 1
  • 1
Sanjeev Rai
  • 6,721
  • 4
  • 23
  • 33
1

If it is really so important to avoid changing the string when you send it, you could chanhe it AFTER you get ir from httprequest. Maybe you could use:

MyUrl = (Request["mt"].Replace(" ","+"));

There is no possibility to pass the space in url, so when you have a space, you can be sure that there was a '+' in there.

Kamil T
  • 2,232
  • 1
  • 19
  • 26
0

You can get the query string using following method

string strQuery =  Request.Url.Query;
Rajeev Kumar
  • 4,901
  • 8
  • 48
  • 83