Section 6.3. Precompile Your Site

6.3. Precompile Your Site

In ASP.NET 1.x, you deploy files with .aspx extensions to the web server that hosts the application. To safeguard your business logic, ASP.NET does not require you to deploy the code-behinds of your Web Forms. However, the user interface of your web application is still encapsulated within the .aspx file, and these files must be deployed to the web server, opening the possibility that someone at the web server end (especially for web hosting scenarios) may read your UI code. ASP.NET 2.0 has gone a step further, allowing you to precompile a site so that when you deploy it:

Note: You can now precompile and deploy your ASP.NET web application without exposing the source, thereby improving first user response time and protecting your IP.
  • There is no code (neither .aspx nor code-behind sources) on the server side.

  • The site is precompiled, which shortens first-use response time.

Tip: If your web application uses client-side scripts, they would still be visible on the user's web browser side, since they have to be sent to the web browser for processing.

6.3.1. How do I do that?

ASP.NET 2.0 supports two precompilation options:

  • Precompile for site deployment

  • Precompile In-Place

Precompile for site deployment allows to you to deploy the compiled binaries of your web site to the hosting machine without the need to deploy your source code. This is a great boost to protecting your source, especially when you are deploying to a remote hosting machine and do not want others to see the source code behind your web site. It also precompiles the site and so reduces the first-use response time of the application.

Let's see how you can precompile a site for deployment.

  1. Launch Visual Studio 2005 and create a new web site project. Name the project C:\ASPNET20\chap06-Precompile.

  2. To precompile the web site, use the aspnet_compiler utility available in the C:\WINDOWS\Microsoft.NET\Framework\<version> folder.

  3. In the command-line window, type the following:

    C:\Windows\Microsoft.NET\Framework\version >aspnet_compiler -v /Precompile -p C:\ASPNET20\chap06-Precompile c:\Precompile_Target 

    The aspnet_compiler Utility

    The syntax of the aspnet_compiler utility is:

    aspnet_compiler [-?] [-m metabasePath | -v virtualPath [-p physicalDir]] [[-u] targetDir]


    The full IIS metabase path of the application. This switch cannot be combined with the -v or -p switches.


    The virtual path of the application to be compiled (e.g., "/MyApp"). If -p is specified, the physical path is used to locate the application. Otherwise, the IIS metabase is used, and the application is assumed to be in the default site (under "/LM/W3SVC/1/Root"). This switch cannot be combined with the -m switch.


    The physical path of the application to be compiled. If -p is missing, the IIS metabase is used to locate the app. This switch must be combined with -v.


    If specified, the precompiled application is updatable.


    Overwrites the target directory if it already exists. Existing contents are lost.


    Suppresses the compiler copyright message.


    The physical path to which the application is compiled. If not specified, the application is precompiled in-place.

  4. Your compiled web site can now be found in C:\Precompile_Target (see Figure 6-11).

    Figure 6-11. The folder containing the compiled page

  5. If you view the content of the placeholder Default.aspx file, you will see that it contains the following single sentence (all the logic is compiled into files located in the bin folder):

    This is a marker file generated by the precompilation tool, and should not be deleted!

  6. To deploy the application to the target web server, you can now create a virtual directory in IIS and point it to the C:\Precompile_Target directory. To access the web site, simply use the following URL format: http://webserver/virtualdir/default.aspx.

6.3.2. What about...

...precompiling in-place?

That's certainly possible. Precompile In-Place allows you to precompile a web site before the user loads the page. Traditional ASP.NET pages are dynamically compiled and cached the first time a user loads the page, so the load time is always higher the first time the site is accessed. With precompilation, the web site is compiled and cached before a user loads a page for the first time, eliminating the long wait for the page to be compiled. Another benefit to this approach is the ability to check for bugs before the user discovers them.

Note: Precompile In-Place is useful for web sites that change often.

To precompile your site before the first user loads it, you simply invoke the special handler precompile.axd located in the virtual root of your web application, like this:


After precompilation, you will notice that there are no delays when the application is first accessed.

Tip: Precompiling works by compiling the entire site (including subdirectories).

...hackers launching a denial-of-service attack at my site by forcing it to constantly precompile?

ASP.NET 2.0 will turn off remote precompiling in-place. You can only perform a precompile in-place locally.

...updating an application once I have precompiled it?

Once you have precompiled an application, you can only deploy the directory that has been generated. To update an application, you would need to modify the original application files and perform the precompilation steps again.

6.3.3. Where can I learn more?

To learn how you can precompile ASP.NET 1.x applications, check out the article at

ASP. NET 2.0(c) A Developer's Notebook 2005
ASP. NET 2.0(c) A Developer's Notebook 2005
Year: 2005
Pages: 104 © 2008-2017.
If you may any questions please contact us: