Introduction to ASP.NET

The ASP.NET infrastructure consists of two main parts :

  • A set of classes and interfaces that enables communication between the Web browser and Web server These classes are organized in the System.Web namespace.

  • A process that handles the Web request for ASP.NET resources This process is also known as the ASP.NET worker process or aspnet_wp.exe .

At a higher level, an ASP.NET Web application is executed through a series of Hypertext Transfer Protocol (HTTP) request and response messages between the client browsers and Web server (see Figure 2.1). The process occurs as follows :

  1. The user requests a resource from a Web server by typing a URL in her browser. The browser sends an HTTP request to the destination Web server.

  2. The Web server analyzes the HTTP request and searches for a process capable of executing this request.

  3. The results of the HTTP request are returned to the client browser in the form of an HTTP response.

  4. The browser reads the HTTP response and displays it as a Web page to the user.

Figure 2.1. When running ASP.NET applications, user computers and the Web server communicate with each other using the HTTP protocol.

graphics/02fig01.gif

As a Web programmer, you will be more interested in knowing what goes on behind the scenes when a Web server executes a Web request for an ASP.NET page (an .aspx file). Figure 2.2 represents the process of server-side execution.

Figure 2.2. The ASP.NET worker process serves the requests to ASP.NET resources using the services provided by the .NET Framework.

graphics/02fig02.gif

Following is a description of the ASP.NET page execution:

  1. When IIS ( inetinfo.exe ) receives an HTTP request, it uses the filename extension of the requested resource to determine which Internet Server Application Programming Interface (ISAPI) program to run to process the request. When the request is for an ASP.NET page (an .aspx file), it passes the request to the ISAPI DLL capable of handling requests for ASP.NET pages, which is aspnet_isapi.dll .

  2. The aspnet_isapi.dll process passes the request to the ASP.NET worker process ( aspnet_wp.exe ), which fulfills the request.

  3. The ASP.NET worker process compiles the .aspx file into an assembly and instructs the CLR to execute the resulting assembly.

  4. When the assembly containing the code for an ASP.NET page executes, it takes the service of various classes in the FCL to accomplish its work and generate response messages for the requesting client.

  5. The ASP.NET worker process collects the response generated by the execution of the Web page, creates a response packet, and passes it to the aspnet_isapi.dll process.

  6. aspnet_isapi.dll forwards the response packet to IIS, which in turn passes the response to the requesting client machine.

Creating an ASP.NET Page

To create an ASP.NET page, all you need to do is write the ASP.NET code in a text file with the .aspx extension and place it in an accessible virtual directory on the Web server. The following example creates a simple ASP.NET Web page that displays a Fahrenheit-to-Celsius temperature conversion chart. Here's how you do it:

  1. Launch Visual Studio .NET. On the start page, click the New Project button (alternatively, you can select File, New, Project). In the New Project dialog box, select Visual C# Projects as the project type and Empty Web Project as the template. Specify the location of the project as http://localhost/ExamCram/315C02 . This sets up the project directory ( 315C02 ) as a virtual directory (within the ExamCram directory) on the default Web site of the local Web server. The project directory is also set up as an IIS application.

  2. Invoke the Solution Explorer window by selecting View, Solution Explorer. Right-click the name of the project and select Add, Add New Item from its shortcut menu, as shown in Figure 2.3. Alternatively, you can select Project, Add New Item from the Visual Studio .NET main menu. In the Add New Item dialog box, select the Text File template and name the file Example2_1.aspx .

    Figure 2.3. Solution Explorer allows easy access to a project and its elements.

    graphics/02fig03.jpg

  3. Write the following code in the HTML view of Example2_1.aspx . (Line numbers are for reference purpose only and are not part of the code.)

     01: <!--Example2_1.aspx --> 02: <%@ Page Language="C#" %> 03: <html><head> 04: <script runat="server"> 05:    double ToCelsius(double f) 06:    { 07:        return  (5.0/9.0)*(f-32.0); 08:    } 09: </script></head> 10: <body> 11: <h2>Fahrenheit to Celsius Conversion Chart</h2> 12:    <table border="2"> 13:       <tr> 14:       <th>&deg; Fahrenheit</th><th>&deg; Celsius</th> 15:       </tr> 16:       <% 17:       for (double f = 50.0; f<=100.0; f++) 18:       { 19:          // Sends formatted output to HTTP response 20:          Response.Output.Write(21:               "<tr><td>{0}</td><td>" + 22:               "{1:f}</td></tr>", f, ToCelsius(f)); 23:       } 24:       %> 25:   </table> 26: </body> 27: </html> 
    graphics/note_icon.gif

    The Response property of the Page class gives you access to the HTTP Response object. You can add content to the HTTP response using the Response.Write() and Response.Output.Write() methods. The only difference between the two methods is that the latter allows you to write formatted output.


  4. In the Solution Explorer, right-click Example2_1.aspx and select View in Browser from its shortcut menu. A browser window opens in Visual Studio .NET and displays the page output shown in Figure 2.4.

    Figure 2.4. The temperature conversion chart is created by executing ASP.NET code on the Web server.

    graphics/02fig04.jpg

In step 3's code, line 1 is the HTML comment line and line 2 is an ASP.NET directive that controls the behavior of the compiler translating the ASP.NET page to MSIL. All ASP.NET code in the page is enclosed in either the <script runat="server">...</script> tag (lines 49) or inside the <%...%> tag (lines 1624).

The Page Class

Prior to execution, each ASPX page is converted into a class. The class corresponding to the ASPX page derives most of its functionality from its base class System.Web.UI.Page . If you look at the .NET Framework documentation, you will note that the Page class itself derives from System.Web.UI.TemplateControl , System.Web.UI.Control , and System.Object .

Because of this inheritance relationship, you can also say that a page is a control, and because the Page class derives from the TemplateControl , it derives extended capabilities for placing other controls on its surface. Table 2.1 lists important members of the Page class.

Table 2.1. Important Members of the Page Class

Member

Type

Description

Application

Property

Returns the application state object for the current HTTP request.

Cache

Property

Returns the cache object of the application to which the page belongs.

ClientTarget

Property

Provides information about the client browser and its capabilities. You can use this information to control the rendering of a page according to particular browser clients .

Controls

Property

Returns a ControlCollection object that represents the collection of controls placed on the page.

EnableViewState

Property

Indicates whether the view state is maintained for the page and its container controls.

Error

Event

Occurs when an unhandled exception occurs in the page processing.

ErrorPage

Property

Specifies the error page where the user is redirected whenever an unhandled exception occurs in the page processing.

Init

Event

Occurs when the page is initialized . This event is the first step in the page life cycle.

IsPostBack

Property

Indicates whether the page is loaded as the result of a client postback.

IsValid

Property

Indicates whether all the validation controls on a page succeeded.

Load

Event

Occurs when the page is loaded in memory.

OnError()

Method

Raises the Error event. It is a protected method derived from the TemplateControl class.

MapPath()

Method

Returns the actual physical path corresponding to a virtual path .

OnInit()

Method

Raises the Init event. It is a protected method derived from the Control class.

OnLoad()

Method

Raises the Load event. It is a protected method derived from the Control class.

OnPreRender()

Method

Raises the PreRender event. It is a protected method derived from the Control class.

OnUnload()

Method

Raises the Unload event. It is a protected method derived from the Control class.

PreRender

Event

Occurs when a page is about to render its contents.

RenderControl()

Method

Outputs content of a page to a provided HtmlTextWriter object and stores tracing information about the control if tracing is enabled.

Request

Property

Returns the request object that contains information about the current HTTP request.

Response

Property

Returns the response object for the current HTTP request.

Server

Property

Provides access to the server object associated with the page.

Session

Property

Returns the session state object applicable for the current HTTP request.

SmartNavigation

Property

Indicates whether smart navigation is enabled for the page.

Trace

Property

Returns the trace context object for the current HTTP request.

Unload

Event

Occurs when a page is unloaded from memory.

Validators

Property

Returns a collection of all validation controls contained in a page.

Visible

Property

Indicates whether the page should be displayed.

Stages in Page Processing

When ASP.NET processes a Web page, the page goes through distinct stages. Some of these common stages are listed in Table 2.2.

Table 2.2. The Stages of ASP.NET Page Processing

Stage

Meaning

Page Initialization

Page initialization is the first stage in the ASP.NET page processing. Its objective is to create the instance of an ASP.NET page. The Init event is raised to indicate this stage. An Init event handler is the best place for code that you want to be executed prior to further page processing.

User Code Initialization

At this stage, the page initialization is complete and the page is loaded into the memory. The Page object indicates this stage by raising the Load event. A method that handles the Load event is the best place to store initialization code for any controls specific to this page.

PreRender

The page is just about to render its contents. The PreRender event is raised to indicate this stage. An event handler for the PreRender event has the last chance to modify the page's output before it is sent to the browser.

Page Cleanup

The page has finished rendering and is ready to be discarded. The Unload event is raised to indicate this stage.



MCAD Developing and Implementing Web Applications with Visual C#. NET and Visual Studio. NET (Exam [... ]am 2)
MCAD Developing and Implementing Web Applications with Visual C#. NET and Visual Studio. NET (Exam [... ]am 2)
ISBN: 789729016
EAN: N/A
Year: 2005
Pages: 191

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net