Query String in ASP.NET

What is a QueryString?
A Query String is a value specified in an HTTP query that can be accessed easily within ASP.NET. The query string is appended at the end of the URL following the question mark(‘?’) character. Multiple query strings can be specified in the URL by separating them by either an ampersand(‘&’) or a semicolon(‘;’).

The following is an example of the query string with a field name of ‘id’ and a value of ‘1’: http://www.mywebsite.com/default.aspx?id=1. Query strings can be used for many different reasons, one common use is to display different data on the same page based on the query string.
For example, if I had an online store and wanted a page to display an inidividual item from my database on the page, we could use a query string. This would work by passing something such as the item’s id in the database as a query string to the page, and then displaying data from the database based on the value of the 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";

Asp.Net Cookies Overview – Easy Guide to Asp.Net Cookies
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"];
}
Advantages of query string :

  • All browsers will support query string and easy to code and use.
  • It does not require any server resource to pass values between pages.

Disadvantages of query string  :

  • Querystring value is visible to the user in url so we should avoid sending any sensitive data and it would allow to send space and & characters through querystring.
  • QueryString does not support passing many values between pages and many browsers supports only 255 characters in the URL.
Reference :

Query String In Asp.Net  - Easy Guide 


Comments

Popular posts from this blog

Top25 ASP.NET Interview Questions - FAQ | Crack the Interview Easily

Top 5 Features of Entity Framework Version 7 Every Dot Net Developers Should Know

The Future of .NET Developement