34
ASP.NET Application Anatomy of ASP.NET application global.asax Application file ASP.NET Configuration .NET Components, Extending the HTTP Pipeline

Ch 04 asp.net application

Embed Size (px)

Citation preview

Page 1: Ch 04 asp.net application

ASP.NET Application

Anatomy of ASP.NET application global.asax Application file ASP.NET Configuration .NET Components,Extending the HTTP Pipeline

Page 2: Ch 04 asp.net application

In traditional desktop programming, an application is an executable file with related support files. (EXE & DLL Files) and other resources such as databases and configuration files.

ASP.NET application is a combination of files, pages, handlers, modules, and executable code that can be invoked from a virtual directory.

Page 3: Ch 04 asp.net application

Anatomy of an ASP.NET Application

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.

Page 4: Ch 04 asp.net application

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.

Page 5: Ch 04 asp.net application

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)

Page 6: Ch 04 asp.net application

Application LifetimeASP.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.

Page 7: Ch 04 asp.net 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.

Page 8: Ch 04 asp.net application

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:\Windows\Microsoft.NET\v2.0.50727\Temporary ASP.NET Files)

ASP.NET’s ability to detect when you change the original files (relies Windows operating system)

Page 9: Ch 04 asp.net application

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.

Page 10: Ch 04 asp.net application

Application Directory Structure cont..

Page 11: Ch 04 asp.net application

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.

Page 12: Ch 04 asp.net application

The global.asax Application File cont..

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.

Page 13: Ch 04 asp.net application

Application EventsDeveloper 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.

Page 14: Ch 04 asp.net application

Application Events cont..

Page 15: Ch 04 asp.net application

Application Events cont..

Page 16: Ch 04 asp.net application

Application Events cont..

Page 17: Ch 04 asp.net application

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

Page 18: Ch 04 asp.net application

ASP.NET ConfigurationConfiguration in ASP.NET is 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

Page 19: Ch 04 asp.net application

The machine.config File

machine.config that resides in the directory c:\Windows\ Microsoft.NET\Framework\v2.0.50727\Config.

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.

Page 20: Ch 04 asp.net application

The web.config FileEvery 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

Page 21: Ch 04 asp.net application

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>

Page 22: Ch 04 asp.net application

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.

Page 23: Ch 04 asp.net application

Configuration Inheritance cont..

Page 24: Ch 04 asp.net application

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>

Page 25: Ch 04 asp.net application

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.

Page 26: Ch 04 asp.net application

The web.config file (<system.web> Settings)

Page 27: Ch 04 asp.net application

The web.config file (<system.web> Settings)

Page 28: Ch 04 asp.net application

The web.config file (<system.web> Settings)<configuration><connectionStrings><add name="NorthwindConnection”

connectionString="Data Source=localhost;Integrated

Security=SSPI;Initial Catalog=Northwind;"

providerName="System.Data.SqlClient" />

</connectionStrings><system.web>...</system.web></configuration>

Page 29: Ch 04 asp.net application

Reading and Writing Configuration Sections Programmatically

Page 30: Ch 04 asp.net application

ASP.NET provides the WebConfigurationManager class in the System.Web.Configuration namespace, which allows you to extract information from a configuration file at runtime.

Page 31: Ch 04 asp.net application

Reading and Writing Configuration Sections Programmatically

For example, 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.we

b/authentication");

Page 32: Ch 04 asp.net application

The Website Administration Tool (WAT)Lets you 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

Page 33: Ch 04 asp.net application

Extending the HTTP PipelineThe 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.

Page 34: Ch 04 asp.net application

Thank you!!!!!