how does Request.QueryString work?

user2560521 picture user2560521 · Aug 21, 2013 · Viewed 121.1k times · Source

I have a code example like this :

 location.href = location.href + "/Edit?pID=" + hTable.getObj().ID; ; //aspx    
 parID = Request.QueryString["pID"]; //c#

it works, my question is - how ? what is the logic ? thanks :)

Answer

Karl Anderson picture Karl Anderson · Aug 21, 2013

The HttpRequest class represents the request made to the server and has various properties associated with it, such as QueryString.

The ASP.NET run-time parses a request to the server and populates this information for you.

Read HttpRequest Properties for a list of all the potential properties that get populated on you behalf by ASP.NET.

Note: not all properties will be populated, for instance if your request has no query string, then the QueryString will be null/empty. So you should check to see if what you expect to be in the query string is actually there before using it like this:

if (!String.IsNullOrEmpty(Request.QueryString["pID"]))
{
    // Query string value is there so now use it
    int thePID = Convert.ToInt32(Request.QueryString["pID"]);
}