Wednesday, 6 July 2016

QueryString



Query String 


Query String is a group of keywords that send request to the web server. These requests are used to pass information (parameters) from one page to another and you can access those information in receiving page. It containing in the HTTP requests for a specific URL. These requests specified by the values following the ? (question mark). The ? (question mark)is used as a separator and it is not part of the query string.


e.g.http://server/program/path/?your_query_string


How to create a Query String ?


You can create a new writeable instance of HttpValueCollection by calling System.Web.HttpUtility.ParseQueryString(string.Empty).


NameValueCollection queryString = System.Web.HttpUtility.ParseQueryString(string.Empty);
queryString["param1"] = "paramValue1";
queryString["param2"] = "paramValue2";


How to retrieve Query String ?


The QueryString collection retrieves the values of the variables in the HTTP query string and it is specified by the values following the ? (question mark).


protected void Page_Load(object sender, EventArgs e) { string param1 = Request.QueryString["param1"]; string param2 = Request.QueryString["param2"]; }


Query String - limitations


Query Strings have some limitations also. Query String have a length limitation because of this limitation when you have to send a lot of information it won't work. Another limitation is that Query string data is directly visible to user so this information can be falsified by malicious users and it leading to security problems.


 Give an example of using querystrings to send data from one page to another?
Query strings are a very simple and popular technique to pass data from one Web page to the next. You send data as part of the URL. In the below example FName and LName are sent as part of the URL. In the page load of QueryStrings2.aspx we use Request.QueryString to read the values. As we are sending more than one query string we use the & symbol to seperate query strings.//Code to send query strings FName and LName as part of the URLQueryStrings2.aspx?FName=David&LName=Boon protected void Page_Load(object sender, EventArgs e)
{
//Code to read Query String values
string FirstName = Request.QueryString["FName"];
string LastName = Request.QueryString["LName"];
Response.Write("Data from QueryStrings1.aspx : " + FirstName + ", " + LastName);
}


Give an example to send Query Strings from code? 


You can send query strings from server side code using the Response.Redirect() method as shown below.Response.Redirect("QueryStrings2.aspx?FName=David&LName=Boon");



What are the advantages of using Query Strings?


1. Query strings are easy to implement.
2. Browser support for passing values in a query string is nearly universal.
3. Query strings are contained in the HTTP request for a specific URL and do not require server resources.


What are the disadvantages of using querystrings to send data from one page to another?


1. Query strings are insecure because the information in the query string is directly visible to the user on the address line in the browser.
2. Many browsers impose a 255 URL character limit which can limit their flexibility.


 

No comments:

Post a Comment