Tutorials » AspNet tutorials »

ASP.NET Request Object


This article talks about the Request object in ASP.NET


Before we go deep into the ASP.NET Request object, you must understand 'What is a Request ?'.

What is a web request ?



In simple words, a web request a 'request sent from a client to the server, asking for a specific web page'.

When you type a URL in a web browser or when you click on a hyper link in any web page, your browser is actually making a 'Request' to the server for the specific URL.

Who makes the Request ?



If you are using a browser to view a web page, then your browser is making the actual request to the server. You simply typed a URL in the browser, but the browser does lot of work in the background. It composes a Request in the proper format that the server can understand and sends the request to the server using the HTTP procol.

Composing the Request



Composing and sending the request involves lot of work, even though a user need not worry about it. It is the responsibilty of the browser to compose and send the request in the proper format.

What is in a Request ?



As you can think, a request obviously includes the URL which is requested. In addition to that, a Request includes several other information, including:

1. Details about the browser who makes the request (like version number, browser type etc)
2. Computer information like screen resolution, IP address of the user etc.
3. Cookies - the information stored in the client machine by the same web site.
4. Data inputted by the user (for example, when you register in a site, you are entering your registration details in the input fields. These details are sent to the server as part of the 'Request')

ASP.NET Request Object



The ASP.NET provides a class called HttpRequest which is defined in the namespace System.Web. This class provides various methods and properties which help you use various information related to a web request.

An instance of this class is created by default in all the pages, so that you can use this object without creating again each time in all the pages. The name of this object is Request.

In old ASP days, the Request object was the only way to retrieve the input data entered by the user in the input fields in the page. ASP.NET provides the event handling mechanism and web controls so that you can access user inputs in a more easy way.

Commonly used Methods and Properies of Request object



Request.FilePath



This property returns the currently executed file path.

Request.QueryString



This method is used to retrieve short key value pairs passed along with the URL. For example, if you can look at the URL of this page, you can see a query string called TutorialId and a value of 35. We pass this query string to indicate which chapter need to be displayed. You may simply change the Tutorial Id in the URL and this page will display the appropriate chapter.

In our ASP.NET code, we will check for the current Tutorial Id using the following code:

' Get the current tutorial Id from the query string.
dim tutorialId as Int32 = Request.QueryString("TutorialId")

' Now get the chapter from database based on the tutorialId.


If the tutorialId is invalid or a chapter not found for the corresponding tutorialid, this page will show an error.

Request.Cookies



Cooikes are used to store small pieces of information in the client computer. Many web sites store information like when did you visit their site last, your user name etc.

When you login to a site and choose the option 'Remember my password', do you know how they remember your password? Your user name is stored in a cookie in your computer.

For example, when you login to this site, if you choose the option 'Remember my password', we will save your user name into a cookie in your computer. If you come back to our site again later, we will read the value from the Cookie as shown below:


Dim cookie As System.Web.HttpCookie = Request.Cookies("AspSpiderUserId")
if cookie is nothing then
Response.write ("Cookie 'AspSpiderUserId' not found.")
else
Response.write ("Value of Cookie 'AspSpiderUserId': " & cookie.Value)
end if


If the user name already exists in Cookie, we understand that you selected the option 'Remember my password' last time. So, we will not ask for your password again. We will automatically login you to the application.

Many sites use Cookies to store more information like when did you visit their site last, what are the pages you visited etc. For example, if you go to an online shopping site and add few items to the shopping cart, they will store this information to the Cookies. When you come back to the same site again, those items will be automatically there in your Shopping cart because they had saved it in your computer cookie and put into your shopping cart when you came back to the site.

Request.ServerVariables



This property can be used to extract specific information about the client who is making the request. For example, if you want to find the server IP address of the computer who is visiting your web page, use the following code:


dim ipAddress as string = Request.ServerVariables("REMOTE_HOST'")
Response.Write ("Your IP Address is : " & ipAddress)


You can find which version of browser they are using to access your web site.


dim browser as string = Request.ServerVariables("HTTP_USER_AGENT'")
Response.Write ("You are using this browser to access our web site: " & browser)


There are several server variables available to find more information from the Request. Try the following server side code in your web page:


dim str as string
For Each str in Request.ServerVariables.AllKeys
Response.write ("Key : '" & str & "' - Value '" & Request.ServerVariables(str) & "'")
Next




Next Chapter: ASP.NET Response object
Previous Chapter: ASP.NET Objects
More Chapters: ASP.NET Tutorials
More Tutorials: Tutorial Index



Top Contributors
Today
    Last 7 Daysmore...

    Awards & Gifts

    Online Members

    More...
     
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India