QueryString Collection in ASP.Net

The QueryString collection is used to retrieve the variable values in the HTTP query string.

The HTTP query string is specified by the values following the question mark (?), like this:

<a href= "MYTestPage.asp?id=24">Link with a query string</a>

The line above generates a variable named id with the value "24".

Query strings are data that is appended to the end of a page URL. They are commonly used to hold data like page numbers or search terms or other data that isn't confidential. Unlike ViewState and hidden fields, the user can see the values which the query string holds without using special operations like View Source.
An example of a query string can look like http://www.MyApplication.com/MYTestPage.aspx?id=24;active=1. Query strings are included in bookmarks and in URLs that you pass in an e-mail. They are the only way to save a page state when copying and pasting a URL.

Query strings are also generated by form submission, or by a user typing a query into the address bar of the browser.

if (Request.QueryString["id"] != null)
{
// Do something with the querystring
}

The only problem with the above check to see if the query string is null, is that we don't take into consideration if the query string is filled or not.

if (!String.IsNullOrEmpty(Request.QueryString["id"]))
{
// Do something with the querystring
}

Then there is the data type of the query string.

The Query String Structure

Query strings are appended to the end of a URL. First a question mark is appended to the URL's end and then every parameter that we want to hold in the query string. The parameters declare the parameter name followed by = symbol which followed by the data to hold. Every parameter is separated with the ampersand symbol.
You should always use the HttpUtility.UrlEncode method on the data itself before appending it.

Query String Limitations

Query string technique when passing from one page to another but that is all. If the first page need to pass non secure data to the other page it can build a URL with a query string and then redirect. You should always keep in mind that a query string isn't secure and therefore always validate the data you received. There are a few browser limitation when using query strings. For example, there are browsers that impose a length limitation on the query string. Another limitation is that query strings are passed only in HTTP GET command.
Tags: , ,
Hot on Web:


About author