Sunteți pe pagina 1din 6

ASP.

NET Objects
This article explains various objects used in ASP.NET programming.
We mentioned in come of the previous chapters that ASP.NET uses object oriented languages like C# or VB.NET. You can use any classes available in the .NET library to write robust programs. ASP.NET provides some Classes which make web development easier. Some of the classes used specifically for ASP.NET development are:

System.Web.HttpRequest System.Web.HttpResponse System.Web.SessionState.HttpSessionState System.Web.ApplicationState

Take the first example - System.Web.HttpRequest. In the above example, HttpRequest is the name of the class, which is defined in the namespace System.Web In object oriented programming, if you want to create an object of a class, you may use a syntax like below: dim mySocket as Socket = new Socket() In the above example, we are creating an object ('mySocket') for the class 'Socket'. Similary, if you need to create an object of class 'System.Web.HttpRequest', it must be something like this:

dim request as System.Web.HttpRequest = new System.Web.HttpRequest() But the ASP.NET system has made your life easier. An object called 'Request' is already created for you, which is ready to use. You do not need to create an object for the class System.Web.HttpRequest. The name of the object is 'Request'. Similary, there are objects created by default for the other classes like HttpResponse, System.Web.SessionState.HttpSessionState and System.Web.ApplicationState. The name of objects are 'Response', 'Session' and 'Application' respectively. In one of the previous chapters, you learned how to display the current time using the Response.Write() method. 'Response' is the name of the object for the class 'HttpResponse' which is created by the system for you. That is why we could use 'Response.Write()' without creating an instance of the class 'HttpResponse'.

The above mentioned 4 objects are widely used in ASP.NET programming. It is important to learn these objects and their methods. You will learn more about these objects in the coming chapters.

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

What is a Response ?
In the web server world, a Response is exactly opposite to the Request. A 'Response' is the message sent from the web server to the client, when client makes a 'Request'. For each request from a client, the server gives a response, unless there is an error. When you type a URL in a web browser or when you click on a hyper link in any web page, your browser makes a 'Request' to the server for the specific URL. Then server process the request and send a response back.

How does a browser process the Response ?


The response includes several information about the page requested including the cookies to be saved on the client machine, the actual content to be displayed to the user etc. The browser accepts the response and processes it. Browser does several things including saving the cookies, checking the security etc and then displays the page content to the user. Note that displaying the page content to the user in the browser is only a small portion of the actual work.

ASP.NET Response Object


The ASP.NET provides a class called HttpResponse 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 response. 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 Response. In old ASP days, the Response object was the only way to write data to the page. ASP.NET provides the event handling mechanism and web controls so that you can write content to the page in an easier way. ------------------------------------------------------------------------------------------------------------Common Gateway Interface (CGI) and the Internet Server Application Programming Interface (ISAPI)

ISAPI is a low level Win32 style API ISAPI is a low level unmanged Win32 API .asmx extension for Web Services

AdRotator The element <Impressions> has a number as a value. This number indicates how often the image is displayed with respect to other images. The more the value of the number in this element, the more is the frequency of the image that is displayed. The sum of all the impressions in the advertisement file should not exceed 2,047,999,999. Otherwise the AdRotator will throw an runtime exception.

Typically a project contains the following content files:

Page file (.aspx) User control (.ascx) Web service (.asmx) Master page (.master) Site map (.sitemap) Website configuration file (.config)

S-ar putea să vă placă și