Você está na página 1de 24

DOT Net Technologies

Unit 5

Unit 5
Structure: 5.1

ASP.NET Applications

Anatomy of an ASP.NET Application Objectives

5.2 5.3 5.4

The Web.config File The Global.asax Application File Summary Self Assessment Questions

5.5 5.6

Terminal Questions Answers to Self Assessment Questions

5.1 Anatomy of ASP.NET Application


To participate in the Web application world, Microsoft developed Active Server Pages (ASP). ASP was a quick and easy way to develop web pages. ASP Pages consisted of a single page that contained a mix of markup and languages. The power of ASP is that you can include VBScript or Jscript code instruction in the page executed on the Web Server before the page was sent to the end users Web browser. This is an easy way to create dynamic Web pages customized based on instructions dictated by the developer. ASP used scripts between brackets and percentage signs - <% %> - to control server-side behaviors. A developer could then build an ASP page by starting with a set of static HTML. Any dynamic element needed by the page was defined using a scripting language. When a user requested the page from the server by using a browser, the asp.dll (an ISAPI application that provides a bridge between the scripting language and the Web server) would take hold of the page and define all the dynamic aspects of the page on-the-fly based on the programming logic specified in the script. After all the dynamic aspects of the page were
Sikkim Manipal University Page No. 156

DOT Net Technologies

Unit 5

defined, the result was an HTML page output to the browser of the requesting client. Before the introduction of .NET, the model that classic ASP provided and what developed in Visual Basic were so different that few VB developers also developed Web applications and few Web application developers also developed the thick client applications of the VB world. There was a great divide. ASP.NET bridged this gap. ASP.NET brought a Visual Basic style eventing model to Web application development, providing much needed state management techniques over stateless HTTP. Its model is much like the earlier VB model in that a developer can drag and drop a control onto a design surface or form, manipulate the controls properties, and even work with the code behind these controls to act on certain events that occur during their lifecycles. What ASP.NET created is best of both models. Goals of ASP.NET ASP.NET is a major release of the product and builds upon the core .NET framework 2.0 with additional classes and capabilities. This release of the framework was code named Orcas internally at Microsoft. ASP.NET 3.5 continues on a path to make ASP.NET developers the most productive developers in the Web space. Ever since the release of ASP.NET 2.0, the Microsoft team has had the goals focused around developer productivity, administration, and management, as well as performance and scalability. New Developer Infrastructures: An exciting aspect of ASP.NET 3.5 is that there are infrastructures in place for you to use in your applications. The ASP.NET team selected some of the most common programming operations performed with Web applications to be built directly into ASP.NET. This saves you considerable time and coding.

Sikkim Manipal University

Page No. 157

DOT Net Technologies

Unit 5

ASP.NET Compilation System The mechanics of the compilation system actually begin with how a page is structured in ASP.NET 3.5. ASP.NET 3.5 offers a different code behind model than the 1.0 / 1.1 because the .NET Framework 3.5 has the capability to work with partial classes (also called partial types). Upon compilation, the separate files are combined into a single offering. This gives you much cleaner code-behind pages. The code that was part of the Web Form Designer Generated section of your classes is separated from the code-behind classes that you create yourself. ASP.NET 3.5 applications can include a \App_Code directory where you place your classs source. Any class placed here is dynamically compiled and reflected in the application. This is just a save and hit deployment model like the one in classic ASP 3.0. Visual Studio 2008 automatically provides IntelliSense for any objects that are placed in the \App_Code directory, whether you are working with the code behind model or are coding inline. ASP.NET 3.5 also provides you with tools that enable you to precompile your ASP.NET applications both the .aspx pages and code behind so that no page within your application has latency when it is retrieved for the first time. Doing this is also a great way to discover any errors in the pages without invoking every page. As you precompile your entire application, you also receive error notifications if any errors are found anywhere within it. Precompilation also enables you to deliver only the created assembly to the deployment server, thereby protecting your code from snooping, unwanted changes, and tampering after deployment.

Sikkim Manipal University

Page No. 158

DOT Net Technologies

Unit 5

ASP.NET Web Pages You use ASP.NET Web pages as the programmable user interface for your Web application. An ASP.NET Web page presents information to the user in any browser or client device and implements application logic using serverside code. ASP.NET Web pages are: Based on Microsoft ASP.NET technology, in which code that runs on the server dynamically generates Web page output to the browser or client device. Compatible with any browser or mobile device. An ASP.NET Web page automatically renders the correct browser-compliant HTML for features such as styles, layout, and so on. Alternatively, you can design your ASP.NET Web pages to run on a specific browser such as Microsoft Internet Explorer 6 and take advantage of browser-specific features. Compatible with any language supported by the .NET common language runtime, including Microsoft Visual Basic, Microsoft Visual C#, Microsoft J#, and Microsoft JScript .NET. Built on the Microsoft .NET Framework. This provides all the benefits of the framework, including a managed environment, type safety, and inheritance. Flexible because you can add user-created and third party controls to them. Components of ASP.NET Web Pages In ASP.NET Web pages, user interface programming is divided into two pieces: the visual component and the logic. If you have worked with tools like Visual Basic and Visual C++ in the past, you will recognize this division between the visible portion of a page and the code behind the page that interacts with it.

Sikkim Manipal University

Page No. 159

DOT Net Technologies

Unit 5

The visual element consists of a file containing static markup such as HTML or ASP.NET server controls or both. The ASP.NET Web page works as a container for the static text and controls you want to display. The logic for the ASP.NET Web page consists of code that you create to interact with the page. The code can reside either in a script block in the page or in a separate class. If the code is in a separate class file, this file is referred to as the code-behind file. The code in the code-behind file can be written in Visual Basic, Visual C#, Visual J#, or JScript .NET. ASP.NET Web pages are compiled into a dynamic-link library (.dll) file. The first time a user browses to the .aspx page, ASP.NET automatically generates a .NET class file that represents the page and then compiles it. The .dll file runs on the server and dynamically produces the HTML output for your page What ASP.NET Web Pages Help You Accomplish? Web application programming presents challenges that do not typically arise when programming traditional client-based applications. Among the challenges are: Implementing a rich Web user interface: It can be difficult and tedious to design and implement a user interface using basic HTML facilities, especially if the page has a complex layout, a large amount of dynamic content, and full-featured user-interactive objects. Separation of client and server: In a Web application, the client (browser) and server are different programs often running on different computers (and even on different operating systems). Consequently, the two halves of the application share very little information; they can communicate, but typically exchange only small chunks of simple information.

Sikkim Manipal University

Page No. 160

DOT Net Technologies

Unit 5

Stateless execution: When a Web server receives a request for a page, it finds the page, processes it, sends it to the browser, and then discards all page information. If the user requests the same page again, the server repeats the entire sequence, reprocessing the page from scratch. Put another way, a server has no memory of pages that it has processedpage are stateless. Therefore, if an application needs to maintain information about a page, its stateless nature can become a problem.

Unknown client capabilities:

In many cases, Web applications are

accessible to many users using different browsers. Browsers have different capabilities, making it difficult to create an application that will run equally well on all of them. Complications with data access: Reading from and writing to a data source in traditional Web applications can be complicated and resourceintensive. Complications with scalability: In many cases Web applications designed with existing methods fail to meet scalability goals due to the lack of compatibility between the various components of the application. This is often a common failure point for applications under a heavy growth cycle. Meeting these challenges for Web applications can require substantial time and effort. ASP.NET Web pages and the ASP.NET page framework address these challenges in the following ways: Intuitive, consistent object mode: The ASP.NET page framework presents an object model that enables you to think of your forms as a unit, not as separate client and server pieces. In this model, you can program the page in a more intuitive way than in traditional Web applications, including the ability to set properties for page elements and
Sikkim Manipal University Page No. 161

DOT Net Technologies

Unit 5

respond to events. In addition, ASP.NET server controls are an abstraction from the physical contents of an HTML page and from the direct interaction between browser and server. In general, you can use server controls the way you might work with controls in a client application and not have to think about how to create the HTML to present and process the controls and their contents. Event-driven programming model: ASP.NET Web pages bring to Web applications the familiar model of writing event handlers for events that occur on either the client or server. The ASP.NET page framework abstracts this model in such a way that the underlying mechanism of capturing an event on the client, transmitting it to the server, and calling the appropriate method is all automatic and invisible to you. The result is a clear, easily written code structure that supports event-driven development. Intuitive state management: The ASP.NET page framework

automatically handles the task of maintaining the state of your page and its controls, and it provides you with explicit ways to maintain the state of application-specific information. This is accomplished without heavy use of server resources and can be implemented with or without sending cookies to the browser. Browser-independent applications: The ASP.NET page framework enables you to create all application logic on the server, eliminating the need to explicitly code for differences in browsers. However, it still enables you to take advantage of browser-specific features by writing client-side code to provide improved performance and a richer client experience. .NET Framework common language runtime support: The

ASP.NET page framework is built on the .NET Framework, so the entire framework is available to any ASP.NET application. Your applications
Sikkim Manipal University Page No. 162

DOT Net Technologies

Unit 5

can be written in any language that is compatible that is with the runtime. In addition, data access is simplified using the data access infrastructure provided by the .NET Framework, including ADO.NET.

.NET Framework scalable server performance The ASP.NET page framework enables you to scale your Web application from one computer with a single processor to a multi-computer Web farm cleanly and without complicated changes to the application's logic.

Structure of an ASP.NET Application A logical way to begin a chapter on ASP.NET applications is to define the term ASP.NET application. An ASP.NET application consists of all the files in a virtual directory and its subdirectories. If your Web server has a subdirectory named MyApp and MyApp is a virtual directory, all the files in MyApp and any subdirectories that stem from it make up an ASP.NET application. Typically, an application includes one or more of the following file types: ASPX files containing Web forms ASCX files containing user controls Web.config files containing configuration settings A Global.asax file containing global application elements DLLs containing custom types employed by the application

An application can contain an unlimited number of ASPX and ASCX files, each representing a different Web page or portion of a page. Only one Global.asax file is permitted. The number of Web.config files isnt restricted, but each must reside in a different directory. ASP.NET places no limit on the number of DLLs an application uses. DLLs are normally found in the application roots bin directory.

Sikkim Manipal University

Page No. 163

DOT Net Technologies

Unit 5

Figure below shows the physical structure of a very simple ASP.NET application that consists of a single Web form in an ASPX file. The directory containing the ASPX file has been transformed into a virtual directory with the IIS configuration manager and is therefore URL-addressable on the server.

ASPX

Figure 5.1 A Simple ASP.NET application

Figure 5.2: A more complex ASP.NET application

Objectives This unit provides an overview of ASP.Net Applications and their development in Visual Studio environment. At the end of this unit, the student would be able to: Discuss the anatomy of an ASP.NET application

Sikkim Manipal University

Page No. 164

DOT Net Technologies

Unit 5

Discuss the usage of Web.config file in a Web Application Discuss the usage of Global.asax file in a Web Application

5.2 The Web.config File


One of the goals of the Microsoft .NET Framework from the outset was to support XCOPY installsthat is, the ability to install applications by copying them to a directory on your hard disk and uninstall them by deleting files and directories. Having this ability means, among other things, that managed applications dont store configuration settings in the registry as traditional Windows applications do. Instead, they store them in text-based XML files. Web.config is the XML file in which ASP.NET applications store configuration data. Heres the general structure of a typical Web.config file:
<configuration> <appSettings> <!-- appSettings values go here --> </appSettings> <system.web> <!-- ASP.NET configuration settings go here --> </system.web> </configuration>

This file is partitioned into two sections: an appSettings section that holds
application-specific data items such as database connection strings, and a system.web section that holds ASP.NET configuration settings. These sections arent the only ones that can appear in a Web.config file, but they are the most common. Web.configs architecture is extensible, enabling developers to define custom sections when circumstances warrant. <appSettings>

Sikkim Manipal University

Page No. 165

DOT Net Technologies

Unit 5

The appSettings section of Web.config holds application-specific values (strings) that are keyed by other strings. Its purpose is to parameterize an applications behavior, and to allow that behavior to be modified without changing any source code. Suppose you coded the following statements into a Page_Load handler:
SqlDataAdapter adapter = new SqlDataAdapter ("select * from titles where price != 0", "server=hawkeye;database=pubs;uid=sa;pwd="); DataSet ds = new DataSet (); adapter.Fill (ds);

The only problem with this code is that if the database connection string changes if the database moves to another machine, for example, or if the user name or password changes you have to modify the code to update the database connection string. If you work in a big company, code modifications probably trigger a mountain of paperwork and require all or part of the application to be retested and reapproved. A better solution to encoding connection strings and other data thats subject to change over the lifetime of an application is to put it in the appSettings section of Web.config. The following Web.config file declares a connection string and assigns it the name MyConnectionString:
<configuration> <appSettings> <add key="MyConnectionString" value="server=hawkeye;database=pubs;uid=sa;pwd=" /> </appSettings> </configuration>

Sikkim Manipal University

Page No. 166

DOT Net Technologies

Unit 5

Page_Load can be rewritten to extract the connection string from Web.config:


string conn = ConfigurationSettings.AppSettings["MyConnectio nString"]; SqlDataAdapter adapter = new SqlDataAdapter ("select * from titles where price != 0", conn); DataSet ds = new DataSet (); adapter.Fill (ds);

AppSettings is a static method belonging to the ConfigurationSettings class in the FCLs System.Configuration namespace. It retrieves values by name from the appSettings section of Web.config. The benefit to doing it this way? Storing the database connection string in Web.config enables you to change it without touching any actual program code. Its analogous to storing program settings in the registry in a Windows application, and it comes with all the perks but none of the drawbacks. <system.web> The system.web section of Web.config holds configuration settings used by ASP.NET. Its content is categorized by subsections. Although the type and number of subsections that can appear is technically unlimitedas developers are free to define custom subsectionsthe ones listed in the following table are supported by default and can be used without writing custom configuration handlers. <system.web> Subsections

5.3 The Global.asax Application File


Global.asax is a text file that houses application-level event handlers, declarations that pertain to all parts of the application, and other global application elements. ASP.NET applications dont have to include Global.asax files, but most do. An application can have only one Global.asax file. That file must be located in the applications virtual root directory.
Sikkim Manipal University Page No. 167

DOT Net Technologies

Unit 5

Whats inside a Global.asax file? Global.asax supports three element types: Global directives Global event handlers Global object tags

Of the three, the first two are used more often. Global event handlers are particularly important and are the number one reason why developers include Global.asax files in their applications. Well discuss global directives first and global event handlers second. Then, for completeness, well talk about global object tags, too. Global Directives Global directives, also known as application directives, provide applicationwide instructions to the ASP.NET compilation engine. A Global.asax file supports three types of global directives: @ Application directives @ Import directives @ Assembly directives

Global.asax can contain just one @ Application directive, but it places no limit on the number of @ Import and @ Assembly directives. The @ Application Directive @ Application directives serve two purposes: they enable developers to add descriptive text to applications, and they facilitate code-behind programming in Global.asax files. An @ Application directive accompanied by a Description attribute adds descriptive text, as in <%@ Application Description="My First ASP.NET Application" %> ASP.NET ignores Description attributes, so descriptions declared with it are visible only to those persons with access to your Global.asax files. The @ Application directive also supports an Inherits attribute that enables code to be removed from Global.asax and packaged in a separate DLL.

Sikkim Manipal University

Page No. 168

DOT Net Technologies

Unit 5

Suppose, for example, you included the following Global.asax file in an application: <%@ Import Namespace="System.Data" %>
<script language="C#" runat="server"> void Application_Start () { DataSet ds = new DataSet (); ds.ReadXml (Server.MapPath ("GlobalData.xml")); Application["GlobalData"] = ds; } </script>

Coded this way, Application_Start, which is an event handler that fires each time the application starts up, is compiled the first time Global.asax is accessed by ASP.NET. To avoid run-time compilation, you can remove Application_Start from Global.asax and code it into a class that derives from System.Web.HttpApplication:
using System.Web; using System.Data; public class MyApp : HttpApplication { public void Application_Start () { DataSet ds = new DataSet (); ds.ReadXml ("GlobalData.xml"); Application["GlobalData"] = ds; }}

Then you compile the CS file into a DLL, place the DLL in the application roots bin directory, and reduce Global.asax to one simple statement: <%@ Application Inherits="MyApp" %> Code-behind offers the same benefits to Global.asax that it offers to ASPX files: it catches compilation errors before the application is deployed, and it

Sikkim Manipal University

Page No. 169

DOT Net Technologies

Unit 5

enables developers to code handlers in C++ and other languages that ASP.NET doesnt explicitly support. A look behind the scenes reveals why code-behind classes used by Global.asax files derive from HttpApplication. ASP.NET starts an application running when the very first request for that application arrives. Starting an application involves launching a process named Aspnet_wp.exe (commonly referred to as the ASP.NET worker process) if it isnt already running and creating a new application domain in that process to host the application and segregate it from other running ASP.NET applications. In the absence of code-behind, startup also involves parsing Global.asax and placing any content found there into a temporary file containing a class derived from HttpApplication, compiling the temporary file into a DLL, and instantiating the derived class. The resulting HttpApplication object handles the request that prompted the application to start up. As a performance optimization, ASP.NET maintains a pool of such objects and uses them to service incoming requests. One implication of this design is that any code you include in Global.asax executes in the context of an HttpApplication object. That means you can call HttpApplication instance methods and access HttpApplication instance properties from anywhere in Global.asax. It also explains why using codebehind in Global.asax means deriving from System.Web.HttpApplication rather than System.Web.UI.Page. Because the system places Global.asax code in an HttpApplication-derived class, you must do the same if you want to get your code out of Global.asax and into a DLL. The @ Import Directive The @ Import directive serves the same purpose in Global.asax that it serves in ASPX files: it imports namespaces that ASP.NET doesnt import by default. For example, lets say you include the following <script> block in Global.asax:

Sikkim Manipal University

Page No. 170

DOT Net Technologies

Unit 5

<script language="C#" runat="server"> void Application_Start () { DataSet ds = new DataSet (); ds.ReadXml (Server.MapPath ("GlobalData.xml")); Application["GlobalData"] = ds; } </script>

Because DataSet is defined in the System.Data namespace and System.Data isnt imported by default, you must either fully qualify all references to DataSet by including the namespace name or place the following directive at the top of Global.asax: <%@ Import Namespace="System.Data" %> @ Import directives in Global.asax pertain only to code in Global.asax. They do not import namespaces into other of the applications files. The @ Assembly Directive The @ Assembly directive does for Global.asax what @ Assembly does for ASPX files: it identifies assemblies Global.asax uses that ASP.NET doesnt link to by default. (As an example, suppose your Global.asax file uses classes in the System.DirectoryServices namespace. Because that namespace isnt imported by default and because the types that belong to that namespace live in System.DirectoryServices.dll, which ASP.NET doesnt link to by default, you need to include the following statements in Global.asax: <%@ Import Namespace="System.DirectoryServices" %> <%@ Assembly Name="System.DirectoryServices" %> If you dont, ASP.NET will greet you with an error message the moment the application starts up.

Sikkim Manipal University

Page No. 171

DOT Net Technologies

Unit 5

Global Event Handlers The most common reason for including Global.asax files in ASP.NET applications is to handle global events events that arent specific to a particular page but that apply to the application as a whole. Some global events are fired by the HttpApplication instances that process individual requests. Others are fired by HTTP modules plug-in components that provide services such as authentication and output caching to ASP.NET. Some events fire on every request. Others fire at predictable junctures in an applications lifetime, such as when the application starts or stops. Still others fire conditionally for example, when an unhandled exception occurs. Regardless of when a global event fires or who fires it, you can process it by including a handler in Global.asax. Start and End Events ASP.NET fires global events named Start and End when an application starts and stops. To process these events, include handlers named Application_Start and Application_End in Global.asax:
<script language="C#" runat="server"> void Application_Start () { ... } void Application_End () { ... } </script>

Application_Start is called when the application receives its first request. This handler is frequently used to initialize application state or the ASP.NET application cache (both of which are introduced later in this chapter) with
Sikkim Manipal University Page No. 172

DOT Net Technologies

Unit 5

data that is global to the application that is, shared by all of its users. Application_End is called when the application shuts down. Typically, that happens when the application has run for 20 minutes without receiving an HTTP request. Application_End isnt used all that of ten because ASP.NET applications dont have to clean up after themselves by deleting objects created in Application_Start, but its sometimes used to write data to a persistent storage medium prior to shutdown so that the data can be reloaded the next time the application starts and to dispose of objects that encapsulate unmanaged resources such as database connections. Later in this chapter, youll learn about ASP.NET session state. Session state is a mechanism for storing per-user information (such as shopping carts) in Web applications and preserving it across requests. Session state services are provided by an HTTP module named SessionStateModule, which fires a Start event each time it creates a session and an End event each time a session ends. You can process these events by including handlers named Session_Start and Session_End in Global.asax:
<script language="C#" runat="server"> void Session_Start () { ... } void Session_End () { ... } </script>

Session_Start is called when a user visits your site who hasnt been there recently (usually in the last 20 minutes). Session_End is typically called when a session times out, which by default happens 20 minutes after the
Sikkim Manipal University Page No. 173

DOT Net Technologies

Unit 5

last request is received from the user for whom the session was created. The most common use for Session_Start is to initialize session state with data that is unique to each user. Per-Request Events Global.asax can also include handlers for events fired by HttpApplication instances. If present in Global.asax, the following methods are called in every request in response to HttpApplication events. Theyre listed in the order in which theyre called.
Method Application_Begin Request Application_Authenticate Request Application_AuthorizeRequest Application_ResolveRequest Cache Application_AcquireRequest State Application_PreRequestHandler Execute Application_PostRequestHandler Execute Application_ReleaseRequest State Application_UpdateRequest Cache Application_EndRequest Description Called at the beginning of each request Called to authenticate the caller Called to determine whether the caller is authorized to access the requested resource Called to resolve the current request by providing content from a cache Called to associate the current request with a session and populate session state Called to prepend content to the HTTP response Called to append content to the HTTP response Called to release (store) associated with this session any state

Called to update a cache with content returned in the response Called at the end of each request

These handlers let you customize ASP.NET by plugging into the request processing pipeline. For example, Application_ResolveRequestCache and Application_UpdateRequestCache could be used to implement a custom output cache. Application_AuthenticateRequest and Application_Authorize Request provide hooks for modifying ASP.NETs security apparatus. The
Sikkim Manipal University Page No. 174

DOT Net Technologies

Unit 5

event handlers Application_PreRequestHandler Execute and Application_ PostRequestHandlerExecute enable HTTP responses to be modified before theyre returned to clients. The following Global.asax file uses the latter of these two methods to place a copyright notice at the bottom of each and every page (assuming, of course, that your pages use HTML flow layout rather than absolute positioning): <script language="C#" runat="server">
void Application_PostRequestHandlerExecute (Object sender, EventArgs e) { HttpApplication app = (HttpApplication) sender; app.Context.Response.Write ("<hr><center><i>" + "Copyright 2002 by Me, Myself, and I</i></center>"); } </script>

Outputting a copyright notice this way rather than duplicating it in every ASPX file lets you change it in one place to modify it everywhere it shows up. Error Events The events listed above fire in each and every request. HttpApplication also defines an Error event that fires if ASP.NET throws an unhandled exception. You can process Error events by including an Application_Error handler in Global.asax. Heres a Global.asax file that logs unhandled exceptions in the NT event log. It uses the FCLs System.Diagnostics.EventLog class to write to the event log:

Sikkim Manipal University

Page No. 175

DOT Net Technologies

Unit 5

<%@ Import Namespace="System.Diagnostics" %>


<script language="C#" runat="server"> void Application_Error (Object sender, EventArgs e) { // Formulate a message to write to the event log string msg = "Error accessing " + Request.Path + "\n" + Server.GetLastError ().ToString (); // Write an entry to the event log EventLog log = new EventLog (); log.Source = "My ASP.NET Application"; log.WriteEntry (msg, EventLogEntryType.Error); } </script>

Its not unwise to include a handler like this one in every ASP.NET application so that you can detect unhandled exceptions by periodically checking the NT event log. You could even modify the handler to send an email message to a system administrator to apprise him or her of unhandled exceptions (a sure sign of a sick or buggy application) the moment they occur. Dont be surprised if you encounter a Global.asax file containing an event handler thats not mentioned here. HttpApplication fires a few other events that I havent listed because theyre rarely used or used internally by ASP.NET. Plus, ASP.NET can be extended with HTTP modules that fire global events of their own. HTTP modules can also sink global events, which is precisely how the HTTP modules built into ASP.NET work much of their magic.

Sikkim Manipal University

Page No. 176

DOT Net Technologies

Unit 5

Global Object Tags Global object tags create object instances declaratively. Suppose you want a new instance of ShoppingCart created for each user that visits your site. Rather than do this:
<script> void Session_Start () { Session["MyShoppingCart"] = new ShoppingCart (); } </script>

you can do this: <object id="MyShoppingCart" class="ShoppingCart" scope="session" Runat="server" /> Assuming ShoppingCart has an Add method, a Web form could add an item to a users shopping cart by doing this: MyShoppingCart.Add (...); This code might not make a lot of sense right now, but itll make plenty of sense by the end of the chapter. An <object> tags Scope attribute assigns a scope to the object instances it creates. Scope=Application creates one object instance, which is shared by all users of the application. Scope=Session creates one object instance per session (that is, per user). Scope=Pipeline creates a unique instance of the object for each and every request. ASP.NET doesnt create objects declared with <object> tags unless it has tothat is, until theyre requested for the first time. Lazy instantiation prevents objects from being created unnecessarily if the application doesnt use them.

Sikkim Manipal University

Page No. 177

DOT Net Technologies

Unit 5

5.4 Summary
This unit introduces the reader with Anatomy of ASP.NET applications including the compilation system, web pages, components of web pages and so on. It demonstrates the various applications that can be developed with ASP.NET. It gices the structure of an ASP.NET application. It demonstrates the configuration file usage of web,.config and global.asax application files. Self Assessment Questions 1. ASP uses ____ between brackets and percentage signs - <% %> - to control server-side behaviors. 2. The _______ file is an ISAPI application that provides a bridge between the scripting language and the Web server. 3. ASP.NET 3.5 applications can include a _______ directory where you place your classs source. 4. In ASP.NET Web pages, the ______ programming is divided into two pieces: the visual component and the logic. 5. A ______ file contains global application elements in ASP.NET. 6. The Global.asax file contains an _______ section that holds applicationspecific data items such as database connection strings

5.5 Terminal Questions


1. Discuss the following: ASP.NET Compilation system Components of ASP.NET Web pages (Refer to 5.1)

2. Describe the applications of ASP.NET Web Pages. (Refer to 5.1) 3. Discuss the following: Web.config file (Refer to 5.2) Global.asax Application File (Refer to 5.3)

Sikkim Manipal University

Page No. 178

DOT Net Technologies

Unit 5

5.6 Answers to Self Assessment Questions


1. scripts 2. asp.dll 3. \App_Code 4. user interface 5. Global.asax 6. appSettings

Sikkim Manipal University

Page No. 179

Você também pode gostar