As you design your Web applications, consider how they will be deployed. If possible, try to minimize the system impact of any installation. This way, you can keep closer track of any changes to the application and ease the problem of deploying updates to an existing application. However, sometimes you will need to perform more complex installations (for example, reusing unmanaged code components or storing sensitive data securely in the Registry).
ASP.NET 2.0 provides a number of new features related to deploying a Web-based application. This article discusses the new features the .NET Framework 2.0 provides for packaging and deploying ASP.NET 2.0 Web applications onto target machines. Along the way, you will learn how to leverage Windows installer technology for creating sophisticated ASP.NET application setups and installers.
From ASP.NET 1.x to ASP.NET 2.0
ASP.NET 1.x versions provided a number of features that were aimed at greatly simplifying the deployment of ASP.NET applications. They relied heavily on the .NET Framework class libraries for much of their functionality because the class libraries could be very easily copied over the assemblies to the bin directory. The .NET Framework 2.0 and Visual Studio 2005 build on the foundation of the .NET Framework 1.x and enhance the deployment features by providing new options.
Before looking at the different deployment options available for deploying an ASP.NET 2.0 application, briefly consider the structure of an ASP.NET application.
Components of an ASP.NET Web Application
A typical ASP.NET application consists of all the Web pages (.aspx and HTML files), handlers, modules, executable code, and other files (such as images and configuration files) packaged into a virtual directory and its subdirectories on a Web server. An ASP.NET application can also include compiled assemblies such as business logic layer assemblies or data access layer assemblies. These assemblies are located in the bin directory underneath the application’s virtual directory.
You can deploy an ASP.NET application onto a target server in a number of ways. The next section discusses each of these options in detail and provides examples.
Deployment Options That .NET Supports
You can deploy an ASP.NET Web application using any one of the following four deployment options:
- XCOPY deployment
- The Copy Web Site option in Visual Studio 2005
- The Precompilation Options in Visual Studio 2005
- Visual Studio 2005 installer
The following sections provide an in-depth look at all of the deployment options available in .NET. For demonstration, create a new Web site by selecting File->New Web Site from the Visual Studio 2005 menu. In the New Web Site dialog box, select Visual Basic as the language and select ASP.NET Web Site from the list of Web site templates. Specify the Web site location as shown in Figure 1.
Figure 1. Specify the Location for Your New Web site
This tutorial uses the preceding project to demonstrate the different ways of deploying a Web application.
One of the primary goals of the .NET framework is to simplify the deployment of Web-based applications by providing XCOPY deployment. XCOPY deployment makes deploying a Web application very simple. Just copy over all the required pages into the root folder and the dependant assemblies into the bin directory and you are done.
In Figure 2, you can see how a Web application named Deployment is xcopied to the c:temp folder.
Figure 2. The Deployment Web Application Xcopied to the c:temp Folder
The xcopy command takes a number of arguments. (For more information on the different options supported by the xcopy command, refer to this MSDN link.)
After the folder is copied over to the target server, create a virtual directory on the target server (using the Internet Information Manager MMC snap-in) and map that virtual directory to the physical directory that you used as the target in the XCOPY command. Now, you can browse to the Web page and start running the pages using a browser.
Using the Copy Web Site Option in Visual Studio 2005
The Copy Project option in Visual Studio .NET makes it very easy for you to deploy ASP.NET Web applications onto target servers. By using this option, you can copy the Web project to the same server or to a different server. Note that when you use this method, this utility simply copies the files from the source location to the target location without performing any type of compilation on those files. This means you need to ensure that the entire Web site compiles correctly before using this approach to copy the files over to the target location.
To copy the Web site onto the target server, select Web Site->Copy Web Site from the menu. Selecting that option will result in the dialog box displayed in Figure 3.
Figure 3. The Resulting Dialog Box from the Copy Web Site Option
The Copy Web Site dialog box provides the following options:
- Destination Project Folder: By clicking on the Connect to... item in the dropdown list, you can open the Open Web Site dialog box and specify the target location. The location can be the same server or a remote server.
- While moving the Web site files to the remote directory, you can perform any one of the following three types of moves. These options are available in the Move Files dropdown list:
- All source Web files to remote Web site
- All remote Web files to source Web site
- Synchronize source and remote Web sites
In the Copy Web Site dialog box, click the icon right next to the Connect to... dropdown box. That will result in the Open Web Site dialog box displayed in Figure 4.
Figure 4. The Resulting Dialog Box from the Open Web Site Option
As you can see, you can deploy your Web site in any of the following locations:
- File system
- Local IIS
- FTP sites
- Remote sites
In the open Web site dialog box, create a virtual directory named TargetDeployment and click Open. Now, in the Copy Web Site dialog box, click the Copy Web Site button. This will result in the Deployment Web site being copied to the target directory.
Precompiling a Web Site Before Deployment
One of the new features of ASP.NET 2.0 Web pages is dynamic compilation, which enables you to easily edit an .aspx page, save the page, and request the page in the browser without having to recompile the Web site. This feature is very similar to the “Just Hit Save” programming model in ASP. Even though this feature can be very powerful and useful when developing the application, dynamic compilation does result in a performance hit during the first request a user makes for the Web page. In that case, it would be beneficial to precompile the Web site and make the compiled Web site available so that the performance of the Web site can be increased. This is exactly what ASP.NET 2.0 Precompilation does. By using this feature, you can precompile the Web site and deploy it onto the server so that when someone requests the Web page for the first time, ASP.NET 2.0 can serve up the page quickly without having to compile the Web site.
Precompilation also can be useful in scenarios where you want to deploy your Web application without the source code. Precompilation also allows you to ensure that the site is compilation error-free. If a page in your site contains a compile-time error and you do not precompile the site, a user may see the error the first time he or she requests the page.
Now that you have an understanding of the concepts of precompilation in a general sense, take a look at the different types of precompilation in the following sections. ASP.NET 2.0 supports two modes of pre-compilation: in-place precompilation and precompilation for deployment.
In-place precompilation allows you to batch-compile all the pages in your Web site. This is essentially what ASP.NET does when someone hits a page for the first time within your application. In-place precompilation offers two important advantages:
- It eliminates the performance hit of batch compiling on the first page request.
- It allows you to find compilation errors before your users do.
In-place precompilation is very easy to do. Simply browse to the root of your Web site and then invoke the special handler named precompile.axd that is present in the root of the virtual directory. For example, to precompile the Web site named Deployment present in http://localhost/MyProjects/Developer/, just enter http://localhost/MyProjects/Developer/Deployment/Precompile.axd in the address bar of the browser. If everything is fine and the Web site is compiled successfully, you will see the output shown in Figure 5.
Figure 5. A Successful Invocation of the Special Handler Precompile.axd
Once you have precompiled your Web site, requests for pages within the site should be fulfilled immediately, without any compilation lag.
Precompilation for deployment
Precompilation for deployment enables you to create an executable version of your entire Web site that can be deployed without any source code, including HTML and other static files. By using this mode of precompilation, you can prevent easy access to the intellectual property your code represents. The resulting set of assemblies and stub files can be deployed to a production server through XCOPY, FTP, Windows Explorer, and so forth.
ASP.NET 2.0 provides a command-line utility called aspnet_compiler.exe that allows you to not only precompile but also deploy the Web site in a single step (see Figure 6). To invoke the ASP.NET precompiler, open the Visual Studio 2005 command prompt and enter the following command:
aspnet_compiler -v /<websitename> -p <source> <destination>
Where <websitename> is the name of the Web site, and <source> and <destination> are file system paths pointing to the location of the source Web site and the location to which the compiled version should be emitted. For the example Web site, the command would look something like the following:
aspnet_compiler -v /Deployment -p c:inetpubwwwrootmyprojectsDeveloperdeployment c:compiled
Figure 6. The Command-line Utility aspnet_compiler.exe Allows Precompilation and Deployment in One Step
To view all of the available options for the ASP.NET precompiler, simply enter the command aspnet_compiler /? in the command prompt. After precompiling the Web site for deployment, navigate to the destination directory in Windows Explorer. You’ll see a Web site with a bin directory containing several assemblies and descriptive files, as well as a number of stub files with the same names as the original pages, but with the code (both HTML and executable code) stripped out. However, if you browse the site, the output will be identical to the original site.
Precompilation using Visual Studio 2005
You also can use Visual Studio 2005 to precompile the Web site and deploy it in a specific location. To accomplish this, select Website->Publish Web Site from the menu, which will result in the dialog box displayed in Figure 7.
Figure 7. The Resulting Dialog Box from the Publish Web Site Option
Clicking on Browse will display the Publish Web dialog box. In the Publish Web Site dialog box, you can choose to deploy the Web site in a file system, local IIS, FTP site, or remote site. If you click OK in the above dialog box, Visual Studio will copy all the required files to the C:PrecompiledWeb directory and you can browse to this directory as any other Web directory.
Deployment Using the Visual Studio 2005 Web Setup Project
This section shows how to use the Visual Studio 2005 Web Setup Project to deploy your Deployment Web site.
Creating a Web setup project using Visual Studio 2005 Installer
Start by adding a new Web Setup Project to your Deployment Web site by selecting File->Add Project-> New Project from the menu. In the New Project dialog box, select Setup and Deployment from the Project Types pane, and then select Web Setup Project in the Templates pane (as shown in Figure 8).
Figure 8. Select Web Setup Project in the Templates Pane
Once you’ve created the project, the first step is to add the output of the primary assembly and the content files of the Deployment Web site to the setup project. To do this, right-click on the DeploymentSetup project in the solution explorer and select Add->Project Output from the context menu. In the Add Project Output Group dialog box, select Deployment from the Project combo box and select Content Files from the list.
Configuring properties through the properties window
Visual Studio 2005 allows you to configure most of the important properties associated with deployment by using the Properties dialog of the Web Setup Project. These properties determine the runtime display and behavior of the Windows installer file. To utilize this function, right-click on the DeploymentSetup project from the solution explorer and select Properties from the context menu to open its properties window. The dialog box shown in Figure 9 appears on the screen.
Figure 9. Select Web Setup Project in the Templates Pane
By using the above screenshot, you can configure properties such as Author, Description, Manufacturer, SupportPhone, and so on. Once you set these properties, they will then be available to the users of the Windows installer at the installation time.
Installing the ASP.NET Web application
Now that you have configured all the properties, build the DeploymentSetup project by right-clicking on the DeploymentSetup project from the solution explorer and selecting Build from the context menu. This will create a DeploymentSetup.msi file that you can use to install the Web application on the target servers. You can kick off the installation by double-clicking on the .msi file from Windows exporer. This will initiate the setup wizard that will walk you through the installation steps. The first step in the installation will look like Figure 10.
Figure 10. The First Step in the Installation of the Web Application on Target Servers
Clicking on Next in the above dialog box results in the dialog box in Figure 11, where you can specify the virtual directory that you will use to host the Web application. Note that in the previous versions of Visual Studio Installer, you had no way to specify the target Web site under which the virtual directory would be created. However, as you can see in Figure 11, Visual Studio 2005 provides you with an option to specify the root Web site that will host the virtual directory. By using the Site dropdown box, you can select the Web site where you want to install the virtual directory.
Figure 11. Option to Specify the Root Web Site That Will Host the Virtual Directory
In the above dialog box, you also can click on the Disk Cost... command button to get an idea of the space required for installing this Web application. Clicking on Next in the above dialog box results in the dialog box shown in Figure 12, which asks you to confirm the installation.
Figure 12. The Dialog Box Asking You to Confirm the Installation
When you click on Next in the above dialog box, the installation will begin and the application will be installed. If the application is successfully installed, you will see the dialog box shown in Figure 13.
Figure 13. The Dialog Box Asking You to Confirm the Installation
Now that the installation is complete, you can see the installed ASP.NET application through the Add/Remove Programs option (that you can access through Start->Settings->Control Panel) on your computer.
What Have You Learned?
This article demonstrated the different ways of packaging and deploying an ASP.NET 2.0 Web application. You also learned how to take advantage of Windows installer technology to create .msi Windows installer files.
Download the Code
To download the accompanying source code for the demo, click here.
About the Author
Thiru Thangarathinam has six years of experience in architecting, designing, developing, and implementing applications using object-oriented application development methodologies. He also possesses a thorough understanding of the software life cycle (design, development, and testing). He holds several certifications, including MCAD for .NET, MCSD, and MCP. Thiru is an expert with ASP.NET, .NET Framework, Visual C# .NET, Visual Basic .NET, ADO.NET, XML Web services, and .NET Remoting. Thiru also has authored numerous books and articles. Contact him at email@example.com.