ASP.NET ApplicationAnatomy of ASP.NETapplication global.asax Application file ASP.NET Configuration .NET Components,Extending the HTTP Pipeline
In traditional desktop programming,an application is an executable filewith related support files. (EXE & DLLFiles) and other resources such asdatabases and configuration files.ASP.NET application is a combinationof files, pages, handlers, modules, andexecutable code that can be invokedfrom a virtual directory.
Anatomy of an ASP.NETApplication Unlike a Windows application, the end user never runs an ASP.NET application directly. The web server has no concept of separate applications - it simply passes the request to the ASP.NET worker process. Web pages that are hosted in the same virtual directory (or one of its subdirectories) execute in the same application domain. A virtual directory is simply a directory that’s exposed through a web server.
The Application Domain An application domain is a boundary enforced by the CLR that ensures that one application can’t influence (or see the in-memory data) of another. The following characteristics are a direct result of the application domain mode. All the web pages in a single web application share the same in-memory resources, such as global application data, per-user session data, and cached data. All the web pages in a single web application share the same core configuration settings. All web applications raise global application events at various stages.
The Application Domain cont.. ASP.NET applications can include all of the following ingredients. Web pages (.aspx files) Web services (.asmx files) Code-behind files A configuration file (web.config) global.asax Other components (components developed or third-party components with useful functionality)
Application Lifetime ASP.NET uses a lazy initialization technique for creating application domains. Means that the application domain for a web application is created the first time a request is received for a page in that application. An application domain can shut down for a variety of reasons, including if the web server itself shuts down. ASP.NET automatically recycles application domains when you change the application.
Application Lifetime cont..ASP.NET application may be periodically recycled when certain thresholds are reached. This model is designed to keep an application healthy And to detect characteristics that could indicate a problem has developed or performance of the application has degraded.
Application Updates Can update your web application without needing to restart the web server and without worrying about harming existing clients. Transits to a new application domain when web.config configuration file is modified. ASP.NET doesn’t actually use the ASP.NET files in the virtual directory. Instead, it uses another technique, called shadow copy, (uses files in c:WindowsMicrosoft.NETv2.0.50727Temporar y ASP.NET Files) ASP.NET’s ability to detect when you change the original files (relies Windows operating system)
Application Directory Structure Every web application should have a well-planned directory structure. Independently from the directory structure designed, ASP.NET defines a few directories with special meanings.
Application Directory Structurecont..
The global.asax Application File The global.asax file allows you to write event handlers that react to global events. Users cann’t request the global.asax file directly. Instead, the global.asax file executes its code automatically in response to certain application events. global.asax doesn’t contain any HTML or ASP.NET tags. It contains methods with specific, predefined names. Every global.asax file defines the methods for a single class—the application class (class derives from HttpApplication) The global.asax file is optional, but a web application can have only one global.asax file.
The global.asax Application Filecont.. global.asax must reside in the root directory of the application, not in a subdirectory. An application event handler is just to use the recognized method name (as opposed to web controls events). ASP.NET automatically calls methods in global.asax when the application event occurs. ASP.NET creates a pool of application objects when your application domain is first loaded and uses one to serve each request.
Application Events Developer can handle two types of events in the global.asax file: Events that always occur for every request. These include request-related and response related events. Events that occur only under certain conditions.
Application Events cont..
Application Events cont..
Application Events cont..
Application Events cont.. Avoid use of use the Application_Error() method to control the appearance of a web page. (without coding painstaking conditional logic) Instead, you would probably configure custom error pages using the web.config file. Application_Error() might be extremely useful for logging an error for future reference or even send an e-mail about error to a system administrator
ASP.NET Configuration Configuration in ASP.NETis managed with XML configuration files. The ASP.NET configuration files have several advantages over traditional ASP configuration They are never locked. They are easily accessed and replicated. They are easy to edit and understand No need of configuration tool
The machine.config File machine.config that resides in the directory c:Windows Microsoft.NETFrameworkv2.0.50727Config. defines supported configuration file sections, configures the ASP.NET worker process, and registers providers that can be used for advanced features such as profiles, membership, and role-based security <processModel> This section allows you to configure how the ASP.NET worker process recycles application domains, <machineKey> This section allows you to set the server-specific key used for encrypting data and creating digital signatures.
The web.config File Every web application inherits the settings from the machine.config file and the root web.config file For example, you might want to set a specific method for authentication, a type of debugging, a default language, or custom error pages. web.config file in a web application can’t override all the settings in the machine.config file
The web.config File cont.. Basic skeletal structure of the web.config file <?xml version="1.0"?> <configuration> <appSettings /> <connectionStrings /> <system.web> <!-- ASP.NET configuration sections go here. --> </system.web> </configuration>
Configuration Inheritance The default machine.config settings are applied first. The web.config settings from the computer root are applied next. This web.config file is in the same Config directory as the machine.config file. If there is a web.config file in the application root A, these settings are applied next. If there is a web.config file in the subdirectory B, these settings are applied next. If there is a web.config file in the subdirectory C, these settings are applied last.
Configuration Inheritance cont..
The web.config file cont.. <customErrors> allows you to configure the behavior of your application in response to various HTTP errors. For Example <customErrors defaultRedirect="standarderror.aspx" mode="RemoteOnly"> <error statusCode="404" redirect="filenotfound.htm"/> </customErrors>
The web.config file cont.. The following is a list of the modes supported for the mode attribute: On: Indicates that custom errors are enabled. If no defaultRedirect attribute is supplied, users will see a generic error. Off: Custom errors are disabled. This allows full error details to be displayed. RemoteOnly: Custom errors are shown only to remote clients while full detailed errors are displayed to local clients.
Reading and Writing Configuration Sections Programmatically
ASP.NET provides the WebConfigurationManager class in the System.Web.Configuration namespace, which allows you to extract information from a configuration file at runtime.
Reading and Writing Configuration Sections Programmatically Forexample, if you’re retrieving information from the <authentication> section, you’ll receive an AuthenticationSection object, as shown here: // Get the configuration for the current web application. Configuration config = WebConfigurationManager.OpenWebConfiguration("/"); // Search for the <authentication> element inside the <system.web> element. AuthenticationSection authSection = (AuthenticationSection)config.GetSection(@"system.web/ authentication");
The WebsiteAdministration Tool (WAT) Letsyou configure various parts of the web.config file using a web-page interface. To run the WAT to configure the current web application in Visual Studio, select Website ➤ ASP.NET Configuration (or Project ➤ ASP.NET Configuration if you’re using projectbased development). Can use the WAT to automate the web.config changes
Extending the HTTPPipeline The pipeline of application events isn’t limited to requests for .aspx web forms. One example is if you want to create a web resource that dynamically renders a custom graphic. In this situation, you simply need to receive a request, check the URL parameters, and then return raw image data as a JPEG or GIF file.