ASP.NET Architecture. Halloween Store application.

36
ASP.NET Architecture ASP.NET Architecture
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    223
  • download

    0

Transcript of ASP.NET Architecture. Halloween Store application.

ASP.NET ArchitectureASP.NET Architecture

Halloween Store applicationHalloween Store application

The Shopping Cart page of the The Shopping Cart page of the HalloweenHalloween Store Store applicationapplication

Components of a web applicationComponents of a web application

Client computer Server computer

Internet

Web browser Web server

• Web applications are a type of Web applications are a type of client/server applicationclient/server application, , where a user at a where a user at a clientclient computer accesses an application computer accesses an application at a at a serverserver computer. computer.

• Client and server computers are connected via the Client and server computers are connected via the Internet.Internet.

Hardware and software components Hardware and software components for web applicationsfor web applications At the client computer, the user works with a WAt the client computer, the user works with a Web eb

browserbrowser, such as Internet Explorer or FireFox, , such as Internet Explorer or FireFox, that provides the user interface for the that provides the user interface for the application.application.

The application runs on the server computer The application runs on the server computer under the control of Wunder the control of Web servereb server software. For software. For ASP.NET web applications, the server must run ASP.NET web applications, the server must run Microsoft’s Web server, Microsoft’s Web server, Internet InformationInternet Information ServicesServices ( (IISIIS).).

The server computer typically also runs a The server computer typically also runs a database management systemdatabase management system, or , or DBMSDBMS, that , that provides access to information stored in a provides access to information stored in a database. To improve performance, the DBMS database. To improve performance, the DBMS can be run on a separate server computer.can be run on a separate server computer.

Hardware and software components Hardware and software components for Web applications (cont.)for Web applications (cont.) The user interface for a Web application is The user interface for a Web application is

implemented as a series of Wimplemented as a series of Web pageseb pages that are displayed in the web browser. that are displayed in the web browser.

Each web page is defined by a WEach web page is defined by a Web formeb form using using HTMLHTML, or , or Hypertext MarkupHypertext Markup LanguageLanguage, which is a standardized set of , which is a standardized set of markup tags.markup tags.

The Web browser and Web server The Web browser and Web server exchange information using exchange information using HTTPHTTP, or , or Hypertext Transfer ProtocolHypertext Transfer Protocol..

A static web page is an HTML document that is the same A static web page is an HTML document that is the same each time it’s viewed. It doesn’t change in response to user each time it’s viewed. It doesn’t change in response to user input.input.

Static web pages are usually simple HTML files that are Static web pages are usually simple HTML files that are stored on the Web server with a file extension of .htm stored on the Web server with a file extension of .htm or .html.or .html.

When a browser requests a static web page, the web server When a browser requests a static web page, the web server retrieves the file from disk and sends it back to the browser. retrieves the file from disk and sends it back to the browser.

How a Web server processes static web How a Web server processes static web pagespages

Client Server

BrowserHTML

file

HTTP request

HTTP response

HTML file

How static web pages workHow static web pages work

A web browser requests a page from a web server by A web browser requests a page from a web server by sending the server an HTTP request message. sending the server an HTTP request message.

The HTTP request includes, the name of the HTML file The HTTP request includes, the name of the HTML file being requested and the Internet address of both the being requested and the Internet address of both the browser and the Web server.browser and the Web server.

A user working with a browser can initiate an HTTP request A user working with a browser can initiate an HTTP request in several ways. One way is to type the address of a web in several ways. One way is to type the address of a web page, called a page, called a URLURL, or , or Uniform Resource LocatorUniform Resource Locator, into the , into the browser’s address area and press Enter. Another way is to browser’s address area and press Enter. Another way is to click a link that refers to a web page.click a link that refers to a web page.

A web server replies to an HTTP request by sending a A web server replies to an HTTP request by sending a message known as an message known as an HTTP responseHTTP response back to the browser. back to the browser.

The HTTP response contains the addresses of the browser The HTTP response contains the addresses of the browser and the server as well as the HTML document that’s being and the server as well as the HTML document that’s being returned.returned.

Processing dynamic pagesProcessing dynamic pages

Client Server

BrowserWeb

serverApplication

server

HTTP request

HTTP response

Webapplication

How dynamic web pages workHow dynamic web pages work

A A dynamic web pagedynamic web page is an HTML document generated by a is an HTML document generated by a web application. Often, the Web page changes according to web application. Often, the Web page changes according to information sent to the Web application by the browser.information sent to the Web application by the browser.

When a Web server receives a request for a dynamic web When a Web server receives a request for a dynamic web page, the server passes the request to an page, the server passes the request to an application application serverserver. .

The application server executes the Web application, which The application server executes the Web application, which generates an HTML document. This document is returned generates an HTML document. This document is returned to the application server, which passes it back to the web to the application server, which passes it back to the web server. The Web server, in turn, sends the document back server. The Web server, in turn, sends the document back to the browser.to the browser.

After the page is displayed, the user can interact with it After the page is displayed, the user can interact with it using its controls. Some of those controls let the user using its controls. Some of those controls let the user postpost the page back to the server, so it’s processed again using the page back to the server, so it’s processed again using the data the user entered.the data the user entered.

Dynamic Web PagesDynamic Web Pages

To determine what application server is used to process a To determine what application server is used to process a request, the Web server looks up the extension of the request, the Web server looks up the extension of the requested file in a list of requested file in a list of application mappingsapplication mappings..

Each application mapping specifies which application Each application mapping specifies which application should be run to process files with that extension. should be run to process files with that extension.

If the file extension is aspx, the request is passed to If the file extension is aspx, the request is passed to ASP.NET.ASP.NET.

If the file extension isn’t in the list of application mappings, If the file extension isn’t in the list of application mappings, the requested file is returned to the browser without any the requested file is returned to the browser without any processing.processing.

The process that begins with the user requesting a web The process that begins with the user requesting a web page and ends with the server sending a response back to page and ends with the server sending a response back to the client is called a the client is called a round tripround trip. .

After a web application generates an HTML document, it After a web application generates an HTML document, it ends. Then, unless the data the application contains is ends. Then, unless the data the application contains is specifically saved, that data is lost.specifically saved, that data is lost.

Application stateApplication state

StateState refers to the current status of the refers to the current status of the properties, variables, and other data properties, variables, and other data maintained by an application for a single maintained by an application for a single user. user.

The application must maintain a separate The application must maintain a separate state for each user currently accessing the state for each user currently accessing the application.application.

HTTP is a HTTP is a stateless protocolstateless protocol– It doesn’t keep track of state between round

trips– Once a browser makes a request and receives

a response, the application terminates and its state is lost

Tracking state in web applicationsTracking state in web applications

ServerFirst HTTP request:The browser requests apage.

Client

First HTTP response:The server returns therequested page and theapplication ends.

Next HTTP request:The browser requests anotherpage. The server has no wayto associate the browser withits previous request.

Web serverBrowser

Browser

Browser

Web server

Web server

Features to maintain stateFeatures to maintain state

ASP.NET uses ASP.NET uses view stateview state to maintain the value of form to maintain the value of form control properties that the application changes between control properties that the application changes between executions of the application. View state is implemented by executions of the application. View state is implemented by default.default.

When a user starts a new session, ASP.NET creates a When a user starts a new session, ASP.NET creates a session statesession state objectobject. It contains a session ID that’s passed . It contains a session ID that’s passed from the server to the browser and back again so that the from the server to the browser and back again so that the server can associate the browser with an existing session. server can associate the browser with an existing session. To maintain To maintain session statesession state, you can add program values to , you can add program values to the session state object. Then, those values are maintained the session state object. Then, those values are maintained between executions of the application.between executions of the application.

When an application begins, ASP.NET creates an When an application begins, ASP.NET creates an application stateapplication state objectobject. To maintain . To maintain application stateapplication state, you , you can add program values to the application state object. can add program values to the application state object. These values are available to all users of the application These values are available to all users of the application and are maintained until the application ends.and are maintained until the application ends.

The Costume page after an order is enteredThe Costume page after an order is entered

The Confirmation pageThe Confirmation page

A simple applicationA simple application

Accepts a costume order. Accepts a costume order. To enter an order, the user selects a costume from the To enter an order, the user selects a costume from the

drop-down list on the Costume page, enters a quantity, and drop-down list on the Costume page, enters a quantity, and clicks on the Order Costume button. Then, the order is clicks on the Order Costume button. Then, the order is written to a file and the Confirmation page is displayed.written to a file and the Confirmation page is displayed.

The costume information is retrieved from a file and stored The costume information is retrieved from a file and stored in a sorted list. The sorted list is used to load the drop-in a sorted list. The sorted list is used to load the drop-down list. Then, when the user selects a costume, the price down list. Then, when the user selects a costume, the price of that costume is retrieved from the sorted list and of that costume is retrieved from the sorted list and displayed on the form.displayed on the form.

This application uses view state to maintain the items in the This application uses view state to maintain the items in the drop-down list when the user posts the page to the server drop-down list when the user posts the page to the server by selecting a costume. by selecting a costume.

This application uses session state to store the costume This application uses session state to store the costume and order data between executions. and order data between executions.

The aspx code for the Costume formThe aspx code for the Costume form

<%@ Page Language=“C#" AutoEventWireup="false" Codebehind="Costume.aspx.cs" <%@ Page Language=“C#" AutoEventWireup="false" Codebehind="Costume.aspx.cs" Inherits="CostumeStore.Costume"%>Inherits="CostumeStore.Costume"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HTML><HEAD><HEAD><title>Costume</title><title>Costume</title></HEAD></HEAD> <body MS_POSITIONING="GridLayout"><body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"><form id="Form1" method="post" runat="server"> <asp:image id="Image1" style="Z-INDEX: 107; LEFT: 9px;<asp:image id="Image1" style="Z-INDEX: 107; LEFT: 9px; POSITION: absolute; TOP: 9px" runat="server"POSITION: absolute; TOP: 9px" runat="server" ImageUrl="banner.jpg"></asp:image>ImageUrl="banner.jpg"></asp:image> <asp:label id="Label1" style="Z-INDEX: 101; LEFT: 10px;<asp:label id="Label1" style="Z-INDEX: 101; LEFT: 10px; POSITION: absolute; TOP: 122px" runat="server">POSITION: absolute; TOP: 122px" runat="server"> Costume:</asp:label>Costume:</asp:label> <asp:dropdownlist id="ddlCostumes" style="Z-INDEX: 102;<asp:dropdownlist id="ddlCostumes" style="Z-INDEX: 102; LEFT: 82px; POSITION: absolute; TOP: 124px"LEFT: 82px; POSITION: absolute; TOP: 124px" runat="server" Width="152px" AutoPostBack="True">runat="server" Width="152px" AutoPostBack="True"> </asp:dropdownlist></asp:dropdownlist> <asp:label id="Label2" style="Z-INDEX: 109; LEFT: 10px;<asp:label id="Label2" style="Z-INDEX: 109; LEFT: 10px; POSITION: absolute; TOP: 156px" runat="server">POSITION: absolute; TOP: 156px" runat="server"> Price:</asp:label>Price:</asp:label> <asp:label id="lblPrice" style="Z-INDEX: 110; LEFT: 82px;<asp:label id="lblPrice" style="Z-INDEX: 110; LEFT: 82px;

The aspx code for the Costume form The aspx code for the Costume form (cont.)(cont.)POSITION: absolute; TOP: 156px" runat="server"></asp:label>POSITION: absolute; TOP: 156px" runat="server"></asp:label> <asp:label id="Label3" style="Z-INDEX: 105; LEFT: 10px;<asp:label id="Label3" style="Z-INDEX: 105; LEFT: 10px; POSITION: absolute; TOP: 189px" runat="server">POSITION: absolute; TOP: 189px" runat="server"> Quantity:</asp:label>Quantity:</asp:label> <asp:textbox id="txtQuantity" style="Z-INDEX: 104;<asp:textbox id="txtQuantity" style="Z-INDEX: 104; LEFT: 82px; POSITION: absolute; TOP: 189px"LEFT: 82px; POSITION: absolute; TOP: 189px" runat="server" Width="48px"></asp:textbox>runat="server" Width="48px"></asp:textbox> <asp:requiredfieldvalidator id="RequiredFieldValidator2"<asp:requiredfieldvalidator id="RequiredFieldValidator2" style="Z-INDEX: 106; LEFT: 134px; POSITION: absolute;style="Z-INDEX: 106; LEFT: 134px; POSITION: absolute; TOP: 192px" runat="server"TOP: 192px" runat="server" ErrorMessage="You must enter a quantity."ErrorMessage="You must enter a quantity." ControlToValidate="txtQuantity">ControlToValidate="txtQuantity"> </asp:requiredfieldvalidator></asp:requiredfieldvalidator> <asp:comparevalidator id="CompareValidator1" style="Z-INDEX: 108;<asp:comparevalidator id="CompareValidator1" style="Z-INDEX: 108; LEFT: 299px; POSITION: absolute; TOP: 192px" runat="server"LEFT: 299px; POSITION: absolute; TOP: 192px" runat="server" ErrorMessage="Quantity must be greater than 0."ErrorMessage="Quantity must be greater than 0." ControlToValidate="txtQuantity" ValueToCompare="0"ControlToValidate="txtQuantity" ValueToCompare="0" Type="Integer" Operator="GreaterThan"></asp:comparevalidator>Type="Integer" Operator="GreaterThan"></asp:comparevalidator> <asp:button id="btnOrder" style="Z-INDEX: 103; LEFT: 10px;<asp:button id="btnOrder" style="Z-INDEX: 103; LEFT: 10px; POSITION: absolute; TOP: 237px" runat="server"POSITION: absolute; TOP: 237px" runat="server" Text="Order Costume"></asp:button>Text="Order Costume"></asp:button> </form></form> </body></body></HTML></HTML>

The aspx code for the Confirmation The aspx code for the Confirmation formform<%@ Page Language=“C#" AutoEventWireup="false"<%@ Page Language=“C#" AutoEventWireup="false"

Codebehind="Confirmation.aspx.cs" Codebehind="Confirmation.aspx.cs" Inherits="CostumeStore.Confirmation"%>Inherits="CostumeStore.Confirmation"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HTML> <HEAD><HEAD> <title>Confirmation</title><title>Confirmation</title> </HEAD></HEAD> <body MS_POSITIONING="GridLayout"><body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"><form id="Form1" method="post" runat="server"> <asp:Image id="Image1" style="Z-INDEX: 105; LEFT: 8px;<asp:Image id="Image1" style="Z-INDEX: 105; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server"POSITION: absolute; TOP: 8px" runat="server" ImageUrl="banner.jpg"></asp:Image>ImageUrl="banner.jpg"></asp:Image> <asp:Label id="lblCostume" style="Z-INDEX: 102; LEFT: 9px;<asp:Label id="lblCostume" style="Z-INDEX: 102; LEFT: 9px; POSITION: absolute; TOP: 128px"POSITION: absolute; TOP: 128px" runat="server"></asp:Label>runat="server"></asp:Label> <asp:button id="btnReturn" style="Z-INDEX: 101; LEFT: 9px;<asp:button id="btnReturn" style="Z-INDEX: 101; LEFT: 9px; POSITION: absolute; TOP: 214px" runat="server"POSITION: absolute; TOP: 214px" runat="server" Text="Return to Order Page"></asp:button>Text="Return to Order Page"></asp:button> </form></form> </body></body></HTML></HTML>

The aspx code for the web formsThe aspx code for the web forms

An aspx file defines an HTML document. An aspx file defines an HTML document. The first line of the HTML document is a Page directive that The first line of the HTML document is a Page directive that

specifies the ASP.NET options.specifies the ASP.NET options. The Codebehind option names the file that contains the C# The Codebehind option names the file that contains the C#

code for the page. This option is used by Visual Studio to code for the page. This option is used by Visual Studio to associate the page with the class that contains the C# code associate the page with the class that contains the C# code for the page.for the page.

The Inherits option names the page class that the page The Inherits option names the page class that the page inherits at runtime. This class is part of the DLL file that’s inherits at runtime. This class is part of the DLL file that’s created when you compile the project.created when you compile the project.

The rest of the aspx file contains the HTML that determines The rest of the aspx file contains the HTML that determines how the page will appear in the browser. The HTML can how the page will appear in the browser. The HTML can include standard HTML tags and special ASP.NET tags. The include standard HTML tags and special ASP.NET tags. The ASP.NET tags begin with ASP.NET tags begin with aspasp: and define ASP.NET Web : and define ASP.NET Web Server controls.Server controls.

The aspx code for the web forms The aspx code for the web forms (cont.)(cont.) The Web Server controls are implemented The Web Server controls are implemented

by classes that are defined by the .NET by classes that are defined by the .NET Framework. ASP.NET Framework. ASP.NET rendersrenders these these controls to standard HTML so the controls controls to standard HTML so the controls can be displayed in the browser. can be displayed in the browser.

The runat="server" attribute that appears The runat="server" attribute that appears in the form and asp tags indicates that the in the form and asp tags indicates that the form and its controls are to be processed form and its controls are to be processed at the server by ASP.NET.at the server by ASP.NET.

The code for the Costume formThe code for the Costume form

public class Costume : System.Web.UI.Page {public class Costume : System.Web.UI.Page { NameValueCollection costumes;NameValueCollection costumes; void Page_Load(Object sender, System.EventArgs e) {void Page_Load(Object sender, System.EventArgs e) { GetCostumes();GetCostumes(); if (!IsPostBack) {if (!IsPostBack) { LoadCostumeDropDownList();LoadCostumeDropDownList(); lblPrice.Text = costumes[ddlCostumes.SelectedIndex];lblPrice.Text = costumes[ddlCostumes.SelectedIndex]; }} }} private void GetCostumes() {private void GetCostumes() { if (Session["Costumes“] == null) {if (Session["Costumes“] == null) { costumes = OrdersDB.GetCostumes();costumes = OrdersDB.GetCostumes(); Session["Costumes“] = costumes;Session["Costumes“] = costumes; } else} else costumes = Session["Costumes“];costumes = Session["Costumes“];

}}}}

The code for the Costume form The code for the Costume form (cont.)(cont.) private void LoadCostumeDropDownList() {private void LoadCostumeDropDownList() { DictionaryEntry Costume;DictionaryEntry Costume; foreach (Costume c in Costumes)foreach (Costume c in Costumes) ddlCostumes.Items.Add(c.Key);ddlCostumes.Items.Add(c.Key); }} private void ddlCostumes_SelectedIndexChanged (Object private void ddlCostumes_SelectedIndexChanged (Object

sender, System.EventArgs e) {sender, System.EventArgs e) { lblPrice.Text = Costumes[ddlCostumes.SelectedIndex];lblPrice.Text = Costumes[ddlCostumes.SelectedIndex]; }} private btnOrder_Click(Object sender, System.EventArgs e) {private btnOrder_Click(Object sender, System.EventArgs e) { Order order = new Order(ddlCostumes.SelectedItem.Text,Order order = new Order(ddlCostumes.SelectedItem.Text, txtQuantity.Text, lblPrice.Text);txtQuantity.Text, lblPrice.Text); Session("Order") = order;Session("Order") = order; OrdersDB.Write(order);OrdersDB.Write(order); Response.Redirect("Confirmation.aspx");Response.Redirect("Confirmation.aspx"); }}}}

The code for the Confirmation formThe code for the Confirmation form

public class Confirmation : System.Web.UI.Page {public class Confirmation : System.Web.UI.Page { private void Page_Load(Object sender, System.EventArgs private void Page_Load(Object sender, System.EventArgs

e) {e) { Order order = Session["Order“];Order order = Session["Order“]; lblConfirm.Text = "Thank you for your order of " +lblConfirm.Text = "Thank you for your order of " + order.Quantity + " " + order.Product +order.Quantity + " " + order.Product + " costume" + ((order.Quantity > 1 ? "s" : " costume" + ((order.Quantity > 1 ? "s" :

"") +"") + " @ " + order.UnitPrice + ".<br><br>" +" @ " + order.UnitPrice + ".<br><br>" + "It will be shipped on " +"It will be shipped on " + Now().Date.AddDays(1) + ".“;Now().Date.AddDays(1) + ".“; }} private void btnReturn_Click(Object sender, private void btnReturn_Click(Object sender,

System.EventArgs e) {System.EventArgs e) {Response.Redirect("Costume.aspx");Response.Redirect("Costume.aspx");

}}}}

The code for the Web formsThe code for the Web forms

The code for a form is stored in a file called a The code for a form is stored in a file called a code-behind filecode-behind file. This code includes event . This code includes event procedures that are called by ASP.NET when the procedures that are called by ASP.NET when the user interacts with the controls on the page.user interacts with the controls on the page.

Each time a page is executed, ASP.NET executes Each time a page is executed, ASP.NET executes the procedure for the Load event of the form. the procedure for the Load event of the form. Within this event procedure, you can test the Within this event procedure, you can test the IsPostBack property of the page to determine if IsPostBack property of the page to determine if the page is being loaded for the first time or if it’s the page is being loaded for the first time or if it’s being posted back to the server.being posted back to the server.

You can use the Session property of the page to You can use the Session property of the page to save items in and retrieve items from the session save items in and retrieve items from the session state object. state object.

The code for the web forms (cont.)The code for the web forms (cont.)

You can use the Redirect method of the You can use the Redirect method of the HttpResponse object associated with the page to HttpResponse object associated with the page to display a form other than the one that’s currently display a form other than the one that’s currently displayed. To refer to this object, you use the displayed. To refer to this object, you use the Response property of the page.Response property of the page.

A web form inherits the System.Web.UI.Page A web form inherits the System.Web.UI.Page class defined by the .NET Framework. This class class defined by the .NET Framework. This class defines the basic functionality for all web pages.defines the basic functionality for all web pages.

NoteNote Since the page is the default object within a code-Since the page is the default object within a code-

behind file, you don’t need to refer to it explicitly behind file, you don’t need to refer to it explicitly to use its properties and methods.to use its properties and methods.

Order and OrdersDB classesOrder and OrdersDB classes

public class Order { public String Name; public String Product; public String Quantity; public int UnitPrice;}

using System.IO;public class OrdersDB { public static string OrderFile = “orders.txt“;“orders.txt“; public static string CostumeFile = “costumes.dat“;

public static NameValueCollection GetCostumes() { NameValueCollection costumes = new NameValueCollection(); FileStream costumeStream = new FileStream(costumeFile, FileMode.Open, FileAccess.Read); BinaryReader costumeReader = new BinaryReader(costumeStream); while (costumeReader.PeekChar()) { string sName = costumeReader.ReadString(); int dPrice = costumeReader.ReadDecimal(); costumes.Add(sName, dPrice); } costumeReader.Close(); return costumes; }

public static void Write(Order order) {public static void Write(Order order) { FileStream orderStream = new FileStream orderStream = new FileStream(ordersFile, FileMode.Append, FileStream(ordersFile, FileMode.Append, FileAccess.Write);FileAccess.Write);

StreamWriter OrderWriter = new StreamWriter OrderWriter = new StreamWriter(OrderStream);StreamWriter(OrderStream);

OrderWriter.WriteLine(order.Name + "," OrderWriter.WriteLine(order.Name + "," ++

order.Product + "," +order.Product + "," + order.Quantity + "," + order.Quantity + "," + order.UnitPrice);order.UnitPrice);

OrderWriter.Close();OrderWriter.Close(); }}}}

Assembly

.aspx

.aspx.cs

.cs

HTML code

C#code-behind

files

Otherclasses

C# compilerC# compiler

.dll

.aspx

To web server

To web server

HTML file

Compiler

Page class

How an ASP.NET application is compiledHow an ASP.NET application is compiled

Compiledpage

.dll

Assembly

.dll

HTMLcode

.aspx

How the files of an ASP.NET How the files of an ASP.NET application are stored and compiledapplication are stored and compiled When you build an ASP.NET application, the VB.NET When you build an ASP.NET application, the VB.NET

compiler compiles the files that contain C# code to create compiler compiles the files that contain C# code to create an assembly. The assembly is stored in a DLL file on the an assembly. The assembly is stored in a DLL file on the web server.web server.

The aspx files that contain the HTML code are not compiled The aspx files that contain the HTML code are not compiled along with the C# code. Instead, those files are stored in along with the C# code. Instead, those files are stored in their original format on the web server.their original format on the web server.

When the user requests an ASP.NET page, ASP.NET:When the user requests an ASP.NET page, ASP.NET: creates a class file from the aspx file for the pagecreates a class file from the aspx file for the page compiles that class file and the code it inherits from the C# compiles that class file and the code it inherits from the C#

assembly into a single assembly that’s stored on disk in a assembly into a single assembly that’s stored on disk in a DLL fileDLL file

executes the DLL to generate the pageexecutes the DLL to generate the page

Components of an ASP.NET applicationComponents of an ASP.NET application

Server

Client

Web server (IIS)

Instantiatedpage class

Instantiatedpage class

GeneratedHTML page

Browser

HTTP request HTTP response

ASP.NET(aspnet_isapi.dll)

CompiledPage (dll)

How ASP.NET applications workHow ASP.NET applications work

When IIS receives an HTTP request for an When IIS receives an HTTP request for an ASP.NET page, it forwards the request to ASP.NET page, it forwards the request to ASP.NET. ASP.NET then creates and executes a ASP.NET. ASP.NET then creates and executes a compiled page that combines the page class with compiled page that combines the page class with the compiled C# code.the compiled C# code.

When the compiled page is executed, an instance When the compiled page is executed, an instance of the ASP.NET page class is generated. Then, of the ASP.NET page class is generated. Then, ASP.NET raises the appropriate events, which are ASP.NET raises the appropriate events, which are processed by the event handlers in the page. processed by the event handlers in the page. Finally, the page generates the HTML that’s Finally, the page generates the HTML that’s passed back to IIS.passed back to IIS.

The ASP.NET page is compiled only the first time The ASP.NET page is compiled only the first time it’s requested. After that, the page is run directly it’s requested. After that, the page is run directly from the DLL file.from the DLL file.

Elements of an ASP.NET applicationElements of an ASP.NET application

HTML pages

.aspxPresentation

layerUser controls

.ascx

Style sheets

.css

Code behindfor .aspx files

.aspx.cs

Business Ruleslayer

Code behindfor usercontrols

.ascx.cs

Other classes

.cs

Databaseclasses

.csDatabase

layer Database

Elements of an ASP.NET applicationsElements of an ASP.NET applications

Presentation layerPresentation layer – HTML pages (.aspx files) that define the layout of each web

page– user controls (.ascx files) that define page elements such as

banners, navigation menus, and data entry forms– style sheets (.css files) that control the overall appearance of

the pages. Business Rules layerBusiness Rules layer

– class files that provide the code-behind functions for each page (.aspx.cs)

– user control (.ascx.cs) to implement the application’s processing requirements

– other classes (.cs), such as classes that represent business entities or implement business rules.

Database layerDatabase layer – the database itself (such as Microsoft SQL Server) as well as

classes that work directly with the database.