How Do I Precompile an ASP.NET Whidbey Application?


NOTE: this article was written against a beta version of the product or technology discussed in this article. There is absolutely no guarantee that the solution or code demonstrated here will work on the final version of the product.


NOTE: the concepts presented in this article are now considered obsolete possibly because better alternatives are available.

UPDATE (2005-12-06): The compilation features presented in this article are obsolete. It's recommended that you look at Web Deployment Projects (http://weblogs.asp.net/scottgu/archive/2005/11/10/430283.aspx) instead.

Unlike Web sites created with Visual Studio .NET 2002 or 2003, there is no need to do a full compile of the project whenever something has changed in your site. The new Code Behind feature and the Code directory used in ASP.NET Whidbey allow you to make changes without recompiling. As soon as the changed page gets requested, the .NET FRamework will recompile the page on the fly.

However, it is sometimes useful to compile the application right after you have deployed it. One reason for this precompilation is speed. Another is code protection. A compiled application does not expose its source in the ASPX pages any longer. There are two ways to compile an application at deployment time: In-place compilation and Precompilation for Deployment

In-Place Compilation

In-place compilation is easy: just request the page /precompilation.axd and the Framework will compile the application in pretty much the same way as it would do if you requested each page individually.

Compilation for Deployment

Compilation for Deployment requires you to use a command line utility called aspnet_compiler. To run this utility, follow these steps:

  1. Open a command prompt and navigate to the folder: Windows\Microsoft.NET \Framework\v1.2.30703. If you have a more recent version of Whidbey installed, modify the version number in the path name so it matches your version.
  2. Execute a command similar to this:

    aspnet_compiler /v /YourWebsiteName -p C:\Inetpub\wwwroot\YourSite
                C:\Inetpub\wwwroot\YourSiteCompiled

In this command, YourWebsiteName is the name of your Web site as you see it in the Internet Information Services management console.
As you can see, I passed both the source folder (C:\Inetpub\wwwroot\YourSite) and the output folder (C:\Inetpub\wwwroot\YourSiteCompiled) as command line parameters. If you want, you can leave out the -p parameter and the filename for the source path. In that case, the tool will try to find the physical location of the Web site itself. For this to run, the Web site needs to be located below the Default Web site (under /LM/W3SVC/1/Root in the IIS MetaBase). There is no need to create the output folder; the utility will create the folder for you if it doesn't exist yet.

As soon as the program is ready, take a look in the output folder. You'll find a Bin folder that contains a bunch of DLLs required for the app. You can also see that all your ASPX have decreased in size and are now all less than 1k. That's because they no longer include any source. These files are really just placeholders. At run-time, code in the DLLs in the Bin folder is run instead, serving up the right page for your requests.

By precompiling your application you can prevent people from viewing or modifying your source. All code is now hosted in DLLs and no longer in easy viewable ASPX pages. However, you can no longer do any "quick fixes" on the server. You'll need the original source to make any changes, even if you just want to change some simple HTML markup.


Where to Next?

Wonder where to go next? You can post a comment on this article. You can read existing comments below or you can post a comment yourself on this article .


Consider making a donation
Please consider making a donation using PayPal. Your donation helps me to pay the bills so I can keep running Imar.Spaanjaars.Com, providing fresh content as often as possible.



Talk Back! Comment on Imar.Spaanjaars.Com

I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.

When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.

If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the QuickDocId of the document.

For more information about the Talk Back feature, check out this news item.