Reading form variables passed in the URL

This page describes how to get the value of URL parameters such as ?id=5432 that have been passed to your webpage in the URL, usually by a form that has been submitted. Maybe the URL requested was:

http://www.mywebsite.com/MyWebPage.aspx?id=5432&size=large

What we are trying to do is to get the value of id and size, extracted out of the URL and into our application.

In fact this is very simple to do. In C# use this:

string id=Request["id"];

and in VB.net, use this:

dim id as string = Request("id")

This gets the value of the parameter named "id" from the URL. The best thing to do is to use this code in your Page_Load event handler to get the value from the variables passed to your web page.

NOTE: You can also use the slightly more verbose:

Request.QueryString("id")

which does exactly the same thing but takes longer to type!

NOTE 2: If the parameter cannot be found in the url then the method returns null.