Sie sind auf Seite 1von 35

Developing Web Applications Using ASP.

NET
Objectives

In this session, you will learn to:


Configure a Web server for deployment Deploy a Web application Implement tracing in Web applications

Ver. 1.0

Slide 1 of 35

Developing Web Applications Using ASP.NET


Configuring a Web Server for Deployment

Web servers accept requests from Web browsers and respond accordingly. To enable a Web server to respond to a browser, you need to configure it. The configuration of a Web server involves performing the following tasks:
Creating a virtual directory Defining Web application URLs Registering ASP.NET file mappings

Ver. 1.0

Slide 2 of 35

Developing Web Applications Using ASP.NET


The Virtual Directory

A virtual directory is created so that the user does not get direct access to the Web application folder residing on the host computer. A virtual directory exposes the components of a Web application to the remote clients and allows them to access your Web application through Web browsers.

Let us see how to deploy a website by creating a virtual directory

Ver. 1.0

Slide 3 of 35

Developing Web Applications Using ASP.NET


The Virtual Directory (Contd.)

After creating the virtual directory, you can configure the application pool for your application. An Application pool can contain one or more applications and allows you to configure the level of isolation between different Web applications. You can configure application pools for your Web applications by using the IIS manager. For this, you need to right-click the virtual directory that is created for the Web application and select the Properties option. You can set the isolation for your application by selecting the appropriate option from the Application Protection drop-down list.

Ver. 1.0

Slide 4 of 35

Developing Web Applications Using ASP.NET


The Virtual Directory (Contd.)

The Properties dialog box provides the following application protection options:
Low (IIS Process) Medium (Pooled) High (Isolated)

Ver. 1.0

Slide 5 of 35

Developing Web Applications Using ASP.NET


Web Application URLs

Users access a Web application by using an easy-to-remember address in the address bar of a Web browser known as the Uniform Resource Locator (URL). A URL is the location of a file on the Web and is unique for every application. To access a Web application, users type the URL for that application in the address bar of a Web browser. The Web browser sends this request to the Web server. The Web server then searches for the application mapped to that URL and sends it back to the browser.

Ver. 1.0

Slide 6 of 35

Developing Web Applications Using ASP.NET


Registering ASP.NET File Mappings

When multiple versions of .NET Framework are installed on the same computer, each version installs an ASP.NET Internet Server Application Programming Interface (ISAPI) extension version associated with it. The ISAPI extension version determines which version of the Common Language Runtime (CLR) will be used by a particular Web application. To configure a Web application to use a particular version, you need to register a script map in the Internet Information Services (IIS).

Ver. 1.0

Slide 7 of 35

Developing Web Applications Using ASP.NET


Registering ASP.NET File Mappings (Contd.)

A script map is a path to the ASP.NET ISAPI version used by the application. When a user requests for a particular Web page, the script map for the application directs IIS to forward the requested file to the appropriate version of the ASP.NET ISAPI for processing. To use a particular version of the .NET Framework, you need to update the script maps so that the request for a Web page is forwarded to the appropriate version of the ASP.NET ISAPI for processing. To update the script map, you need to use the ASP.NET registration tool called aspnet_regiis.exe.

Ver. 1.0

Slide 8 of 35

Developing Web Applications Using ASP.NET


Deploying a Web Application

The process of installing a Web application on a Web server is known as deployment. The most basic technique for deployment is to copy the application components to the hard drive of the Web server. This type of deployment has the following issues:
The application is deployed even if it contains compilation errors. The source code of the application is exposed. The application loads slowly initially because it is compiled when it is accessed for the first time.

These problems can be resolved by precompiling an application before deploying it to a Web server.

Ver. 1.0

Slide 9 of 35

Developing Web Applications Using ASP.NET


Precompiling a Web Application

Precompilation involves compilation of the source code of the Web application into DLL assemblies before deployment. Advantages of precompiling a Web application are:
Provides faster response time Helps in identifying the bugs that can occur when a Web page is requested Hides and secures the source code of a Web application from users accessing the application

You can precompile a Web application by using the aspnet_compiler.exe command-line tool.

Ver. 1.0

Slide 10 of 35

Developing Web Applications Using ASP.NET


Precompiling a Web Application (Contd.)

To compile a website, you need to execute the aspnet_complier command, as shown in the following figure.

Ver. 1.0

Slide 11 of 35

Developing Web Applications Using ASP.NET


Precompiling a Web Application (Contd.)

The following table lists some options that can be used with aspnet_compiler.exe tool.
Option -v -p -u Description Specifies the virtual path of the application to be compiled. Specifies the physical path of the application to be compiled. Specifies that aspnet_compiler.exe should create a precompiled application that allows subsequent updates of contents such as .aspx pages. Specifies that the application to be compiled should be fully rebuilt.

-c

Ver. 1.0

Slide 12 of 35

Developing Web Applications Using ASP.NET


Managing ASP.NET Precompiled Output for Deployment

The compilation tool of ASP.NET, aspnet_compiler.exe, compiles the source files and creates separate assemblies for each source file. The assemblies are then combined and managed by using the ASP.NET merge tool, aspnet_merge.exe. The ASP.NET merge tool can be used to combine:
All assemblies in a precompiled website into a single assembly. All Web user interface content assemblies into a single assembly. All Web user interface content assemblies into an assembly for each folder in the website.

Ver. 1.0

Slide 13 of 35

Developing Web Applications Using ASP.NET


Managing ASP.NET Precompiled Output for Deployment (Contd.)

When you use the aspnet_merge tool, without any options, and only specify the target folder name, as shown in the following figure, output assemblies are created for each Web user interface content folder.

Ver. 1.0

Slide 14 of 35

Developing Web Applications Using ASP.NET


Managing ASP.NET Precompiled Output for Deployment (Contd.)

To create a single assembly that combines all Web user interface content assemblies, you need to use the w switch with the aspnet_merge tool and specify a name for the merged assembly, as shown in the following figure.

Ver. 1.0

Slide 15 of 35

Developing Web Applications Using ASP.NET


Managing ASP.NET Precompiled Output for Deployment (Contd.)

To create a single assembly that combines all assemblies in a precompiled website, you need to use the o switch with the aspnet_merge tool and specify a name for the merged assembly, as shown in the following figure.

Ver. 1.0

Slide 16 of 35

Developing Web Applications Using ASP.NET


Managing ASP.NET Precompiled Output for Deployment (Contd.)

Microsoft Visual Studio provides the following methods to deploy a Web application:
Copy Web Site utility Publish Web Site utility Web Setup project

Let us see how to deploy a website by using the Copy Web Site utility
Let us see how to deploy a website by using the Publish Web Site utility Let us see how to deploy a setup project by using a Web Setup project

Ver. 1.0

Slide 17 of 35

Developing Web Applications Using ASP.NET


Debugging a Deployed Application

You can debug an application even after it has been deployed to a Web server.

Let us see how to debug a deployed application

Ver. 1.0

Slide 18 of 35

Developing Web Applications Using ASP.NET


Implementing Tracing in Web Applications

To ensure that the application is working properly and is not giving unexpected results, you need to keep track of the execution of your Web application. ASP.NET provides you with the tracing feature that enables you to track the program execution, thereby ensuring that your Web application runs properly. You can use tracing feature to:
View diagnostic information about a particular Web page. Display custom tracing information about the execution of an application.

Ver. 1.0

Slide 19 of 35

Developing Web Applications Using ASP.NET


Implementing Tracing

Tracing can be implemented at any of the following levels:


Page level Application level

Ver. 1.0

Slide 20 of 35

Developing Web Applications Using ASP.NET


Implementing Tracing (Contd.)

Page-level tracing:
Page-level tracing is used to generate diagnostic information at the end of page rendering. The diagnostic information includes all the information about the requests made to the page and their responses. To enable page-level tracing, you need to perform the following steps:
1. Include the following directive at the top of the page:
<%@ Page Trace="True" %>

2. Include the TraceMode attribute in the @ Page directive to specify the sort order for your trace messages.

Ver. 1.0

Slide 21 of 35

Developing Web Applications Using ASP.NET


Implementing Tracing (Contd.)

The following information is displayed when page-level tracing is enabled:


Request details Trace information Control tree Session state Application state Request Cookies Collection Response Cookies Collection Headers Collection Response Headers Collection Form Collection Querystring Collection Server Variables

Ver. 1.0

Slide 22 of 35

Developing Web Applications Using ASP.NET


Implementing Tracing (Contd.)

Application-level tracing:
Application-level tracing is used to trace information for every Web page in a Web application. A special page named trace.axd is used to view this trace information. To use the trace.axd page, you have to first enable application-level tracing within the Web.config file for your application. You can use the following attributes of the <trace> element to change the tracing settings of your website:
enabled requestLimit pageOutput traceMode localOnly mostRecent
Ver. 1.0

Slide 23 of 35

Developing Web Applications Using ASP.NET


Writing Trace Information

When a Web application is in the development stage, you often use several Response.Write statements in the program code to display debugging information, which is used to troubleshoot the application. The messages displayed using Response.Write statements are required for debugging and should not be displayed to an end user. The process of tracing enables you to insert debugging code within an application such that the debugging code does not need to be removed at the time of deployment. The Trace property of the Page class returns a Trace object, which can be used to write custom trace statements.

Ver. 1.0

Slide 24 of 35

Developing Web Applications Using ASP.NET


Writing Trace Information (Contd.) The Trace object:
Is an instance of the TraceContext class. Provides a set of methods and properties that help you to trace the execution of your application.

The properties of the Trace object are:


IsEnabled: Denotes whether tracing is enabled for the current request. TraceMode: Sets the trace modes such as sortByCategory or sortByTime.

Some of the methods of the Trace object are:


Warn: Displays the trace information in red color. Write: Displays the trace information in black color.

Ver. 1.0

Slide 25 of 35

Developing Web Applications Using ASP.NET


Using Trace Listeners

Trace listeners are used to:


Collect, store, and route tracing messages. Redirect the tracing information to logs, windows, or text files.

ASP.NET provides three types of predefined trace listeners:


TextWriterTraceListener EventLogTraceListener DefaultTraceListener

Ver. 1.0

Slide 26 of 35

Developing Web Applications Using ASP.NET


Using Trace Listeners (Contd.)

Adding a trace listener:


To add a trace listener in the web.config file, you need to include the following markup in the web.config file:
<configuration> <system.diagnostics> <trace> <listeners> <add name="MyListener" type="System.Diagnostics. TextWriterTraceListener" initializeData="D:\myListener.txt" /> </listeners> </trace> </system.diagnostics> </configuration>

Ver. 1.0

Slide 27 of 35

Developing Web Applications Using ASP.NET


Using Trace Listeners (Contd.)

To enable ASP.NET to route tracing information to a System.Diagnostics listener, you need to include the following entry in the web.config file:
<system.web> <trace writeToDiagnosticsTrace = true /> <customErrors mode=off /> </system.web>

Ver. 1.0

Slide 28 of 35

Developing Web Applications Using ASP.NET


Using Trace Listeners (Contd.)

Removing a trace listener:


To remove a trace listener from the listeners collection, you need to include the following markup in the web.config file:
<configuration> <system.diagnostics> <trace> <listeners> <remove name="MyListener"/> </listeners> </trace> </system.diagnostics> </configuration>

Ver. 1.0

Slide 29 of 35

Developing Web Applications Using ASP.NET


Trace Switches

You can control the tracing output by using trace switches. Trace switches can be configured to provide filtered tracing output. You can also enable and disable certain tracing output using trace switches. The two types of trace switches are:
BooleanSwitch TraceSwitch

To initialize a TraceSwitch, you need to first create its object. The following code snippet shows how you can create and initialize a TraceSwitch:
System.Diagnostics.TraceSwitch MyTraceSwitch = new System.Diagnostics.TraceSwitch("MySwitch", "Entire application");
Ver. 1.0

Slide 30 of 35

Developing Web Applications Using ASP.NET


Activity 14.1: Implementing Tracing

Problem Statement:
Your team leader wants you to implement tracing on the MusicMania website. You are instructed to display the trace data for all the Web pages on the MusicMania website. The trace data should not be displayed on individual Web pages. It should be displayed on the host Web server for a maximum of 50 recent requests. You also need to save the trace data in a text file.

Ver. 1.0

Slide 31 of 35

Developing Web Applications Using ASP.NET


Activity 14.1: Implementing Tracing (Contd.)

Solution:
To implement tracing in the MusicMania website, you need to perform the following tasks:
1. Modify the web.config file. 2. Verify the application.

Ver. 1.0

Slide 32 of 35

Developing Web Applications Using ASP.NET


Summary

In this session, you learned that:


The configuration of a Web server involves performing the following tasks:
Creating a virtual directory Defining Web application URLs Registering ASP.NET file mappings

A virtual directory is created so that the user does not get direct access to the Web application folder residing on the host computer. A virtual directory exposes the components of a Web application to the remote clients and allows them to access your Web application through Web browsers. An Application pool can contain one or more applications and allows you to configure the level of isolation between different Web applications.

Ver. 1.0

Slide 33 of 35

Developing Web Applications Using ASP.NET


Summary (Contd.)

A URL is the location of a file on the Web. It is unique for every application. Precompilation involves compilation of the source code of the Web application into DLL assemblies before deployment. Precompiling a Web application provides the following advantages:
It provides faster response time because the Web application does not need to be compiled the first time it is accessed. It helps in identifying the bugs that can occur when a Web page is requested. These bugs are rectified at the time of compiling the Web application. Then the compiled and error-free Web application is deployed on to the server and is rendered to a user. It hides and secures the source code of a Web application from users accessing the application.

The compilation tool of ASP.NET, aspnet_compiler.exe, compiles the source files and creates separate assemblies for each source file.
Ver. 1.0

Slide 34 of 35

Developing Web Applications Using ASP.NET


Summary (Contd.)

Tracing can be implemented at any of the following levels:


Page level Application Level

The properties of the Trace object are:


IsEnabled TraceMode

Some of the methods of the Trace object are:


Warn Write

ASP.NET provides three types of predefined trace listeners:


TextWriterTraceListener EventLogTraceListener DefaultTraceListener

Ver. 1.0

Slide 35 of 35

Das könnte Ihnen auch gefallen