Learning
Learning

Reputation: 20041

QueryString always return null after server.UrlDecode

My page url is very simple EmailToFriend.aspx?PID=5&Lang=en-US

    string _pid= Server.UrlDecode(Request.QueryString["PID"]);
    string _lang = Server.UrlDecode(Request.QueryString["Lang"]);

result:

_pid=5
_lang= null

I am trying to get QueryString value of both PID and Lang but it return null for lang value while it is present in the url i.e= en-US

While debugging i noticed that it show url as PID=5&amp%3bLang=en-US for following line of code

string _lang = Server.UrlDecode(Request.QueryString["Lang"]);

I am not sure what is wrong and why it is showing %3b in place of ;

How can i resolve this so that i get the value for QueryString lang

If i use only & in my url then it works fine with request["lang"]; problem only happens when i encode url & try to decode it back

article reference http://msdn.microsoft.com/en-us/library/6196h3wt%28v=vs.110%29.aspx

doesn't work for me..

Upvotes: 0

Views: 1277

Answers (1)

IronGeek
IronGeek

Reputation: 4883

First, from Wikipedia: a query string is the part of a (URL) that contains data to be passed to web applications, it is encoded as follows:

field1=value1&field2=value2&field3=value3...

  • The query string is composed of a series of field-value pairs.
  • Within each pair, the field name and value are separated by an equals sign, '='.
  • The series of pairs is separated by the ampersand, '&'...

Thus, when you have a url as follows:

EmailToFriend.aspx?PID=5&Lang=en-US

You have a querystring with two field-value pairs, separated by ampersand ('&'):

"PID" => "5", and "amp;Lang" => "en-US"

...

Now, back to your problem:

  1. I am trying to get QueryString value of both PID and Lang but it return null for lang value while it is present in the url i.e= en-US

    The reason why Lang always returns null, is because the key does not exist in the qiven URL, instead the key amp;Lang exist with the value en-US. You can verify this using the following code:

    string _amplang = Server.UrlDecode(Request.QueryString["amp;Lang"]);
    
  2. How can i resolve this so that i get the value for QueryString lang

    You've basically answered this one yourself. Its by fixing your URL and use the correct form of querystring: EmailToFriend.aspx?PID=5&Lang=en-US

  3. article reference http://msdn.microsoft.com/en-us/library/6196h3wt%28v=vs.110%29.aspx doesn't work for me..

    It does work, but unfortunately you're working it wrong. The URL encoded value of ampersand ('&') is %26 not &. The HTML encoded value of ampersand ('&'), on the other hand, is &. You're probably trying to URL-decode an HTML-encoded value.

    For example, let's say you have the following URL:

    EmailToFriend.aspx?PID=5&Lang=en-US&Url=EmailToFriend.aspx%3fPID%3d5%26Lang%3den-US

    If you run the following code:

    string _pid= Server.UrlDecode(Request.QueryString["PID"]);
    string _lang = Server.UrlDecode(Request.QueryString["Lang"]);
    string _url = Server.UrlDecode(Request.QueryString["Url"]);
    

    You'll have the following value:

    _pid=5
    _lang= en-US
    _url = EmailToFriend.aspx?PID=5&Lang=en-US
    

    The Url value is correctly decoded from the query string.

Upvotes: 1

Related Questions