Objects in ASP.NET

Objects in ASP.NET 





Objects in ASP.NET












The bulk of the .NET Framework consists of a huge library of object classes, and just about anything we do in ASP.NET will make substantial use of objects, which have been defined by these classes.  One of the dominant characteristics of .NET is that it defines everything—from intrinsic variables, right up to full-blown applications—as explicit objects.

The Page Class
To understand the role of the Page class, it is important to have a picture of what is going on when we request an ASP.NET page from the web server.  IIS hands over most of the hard work involved to the .NET framework.  When a browser initially calls up an ASP.NET page IIS recognizes that this is an ASPX file request, and lets the ASP.NET module(aspnet_isapi.dll) deal with it.  The aspnet_isapi.dll places the ASPX file we request into a new class definition.  This new class is defined in a namespace called ASP; so the contents of a file called mypage.aspx end up in a class called ASP.mypage_aspx.

The new ASP class is then instantiated as an object in the CLR The Common Language Runtime(CLR)-this is a complex system responsible for executing the MSIL code on the computer.

  A render method is then called on our new object, that returns appropriate HTML via the aspnet_isapi.dll to IIS, which then sends the HTML to the client that originally made the request. 


So that is how .NET gets us from an ASPX file on the server to an HTTP response full of useful HTML.  Our ASP class inherits from the Page class.  This means that our ASP.NET page has access to the useful functionality that the Page class provides.

The Page Class lives in the System.Web.UI namespace.

This allows us to
·       Redirect users to another page
·       Find out about the web browser our visitor is using
·       Find out what web site the person was on before they came to ours
·       Personalize pages for our visitors
·       Store commonly used information centrally


ASP.NET  Core Objects

Request – gives us access to information about the person or process requesting the web page
Response – provides a way for us to accurately control how the Response is sent back to the person who made the Request
Server – provides a range of useful web-related utilities
Application – implements a useful site-wide storage location for frequently used information
Session – makes it possible for us to store information for each user’s session

Each of these objects is created from a class.  For example, although we refer to the Response object, it is created from the System.Web.HttpResponse class.  The reason we call it the Response object is because the specific instance of the HttpResponse Class that we are interested in is accessible using a property of our Page object called Response.
You can see that the Page class provides a range of properties, including Response, Request, and Server.  When we access the Response property, it returns an object of the type HttpResponse.  We can then use methods from that object, such as Redirect().

Response Object


The Response object provides access to the HTTP Response that is going to be sent back to the requesting web browser.  This response includes the HTML for the requested page
The Response object is of the type HTTPResponse.  The HTTPResponse class provides a range of properties and methods for us to use, including the Write() and Redirect() methods.

When asp.net is running the code in our pages, it gradually builds up the HTML that will be sent back to the browser.  Asp.net has what is called a buffer; as the HTML is generated, it is placed in the buffer.  Normally the HTML is held in the buffer so that it is not sent to the browser until the page finishes executing; however, we can change that behavior if we want. 

Buffer The default value of this property is True, which means the page is buffered and sent in one block.  If we set it to False, the Response will be sent piecemeal along the wires as and when each piece is generated. 


ContentType With this property, we can set the type of data we are sending back.  This setting specifies shat is called a MIME type(Multipurpose Internet Mail Extensions, is a way of identifying the different kinds of resources that Internet services like the Web and e-mail can transfer.

Cookies The use of this property is how we save visitors’ settings to their hard drive
Clear() If we call this method, the buffer will be emptied and the contents discarded, but we can continue and add more HTML to it if we want
Flush()When we call this method all the HTML in the buffer is sent to the web browser, but we can continue creating HTML
End()This command is terminal; it sends all the HTML from the buffer and our page stops executing
Redirect()It redirects to another page
Write()It writes a string to the HTML stream
WriteFile() It writes the  contents of a file to the HTML output stream......







0 commentaires: