ASP.NET 2.0 Options for Packaging and Deploying Web Apps, Page 2
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.
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:\inetpub\wwwroot\myprojects\Developer\deployment 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.