Click here to Skip to main content
13,897,867 members
Click here to Skip to main content
Add your own
alternative version


43 bookmarked
Posted 20 Jan 2004

Pre-Compile ASPX pages in .NET 1.1

, 30 Mar 2004
Rate this:
Please Sign up or sign in to vote.
An article on pre-compiling ASPX pages to improve start-up performance.


The first time a HTTP request is made for a ASPX page, then the ASP.NET runtime converts the ASPX page to a '*.cs' file and compiles it into a DLL. This DLL is typically stored in the temporary folder 'C:\WINNT\Microsoft.NET\Framework\v1.1.4322\ Temporary ASP.NET Files'. Because of this, the first response time for a page is always on the higher side. In this article, we shall see how to pre-compile the ASPX pages of an application after installation so that users of the application do not feel the initial delay during compilation of ASPX pages.


The technique that we are going to use is very simple. We would simulate HTTP requests to all the ASPX pages in an application. This would result is the pages getting compiled. We would also pass a special request parameter with each request that would prevent unnecessary processing of all the code in ASPX pages.

How to make HTTP requests?

Writing code to fire HTTP requests for a web resource is very simple. We make use of the HttpWebRequest and HttpWebResponse classes available in the .NET framework. The code snippet below shows how to make a HTTP request for a ASPX page.

HttpWebRequest myHttpWebRequest = null;
HttpWebResponse myHttpWebResponse = null;
myHttpWebRequest = 
myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

About the Global.asax file

The Global.asax file contains global event handlers that get called for important events such as Session_Start, Application_Start, Application_BeginRequest etc. We are interested in the Application_BeginRequest event. This event is the first event in the HTTP pipeline chain of execution when ASP.NET responds to a request. We shall see further how we can make use of this event handler code for our pre-compilation tool.

The Pre-Compilation Tool

The Pre-Compilation tool that we are going to write is going to be a aspx page named PreCompile.aspx. This page needs to be placed in the root directory of the application we want to compile. In the Page_Load() event handler of this page, we simulate requests to all the ASPX pages in that application. This tool can be run by just pointing the browser to http://{host}/{virtual_root or application_root}/PreCompile.aspx.

Note: ASP.NET is optimized to do a batch-compile of all pages in a folder if any page in that folder is requested. But there is no guarantee that batch-compilation would always occur. It depends on the load on the server at that point of time. So it's safe to make requests to all pages in an application.

But how do we get URLs of all the pages in that application? We can get the physical path of the root folder of the current application through the PhysicalApplicationPath property of the request object. Once we get the physical folder path on the server, we can write a recursive function that would give us a list of all ASPX files in that application. The code snippet below demonstrates how this is done:

string rootDirectory = Request.PhysicalApplicationPath;
ArrayList arrayList = GetAllFiles(rootDirectory, 
  "*.aspx",true);//gets a list of all aspx files in a folder.

Then we construct URLs for each of the ASPX pages. For these HTTP requests, we make the host as 'localhost' since we are running the tool on the server. We get the virtual root of the application from the raw URI of the request. So, to construct a URL, we append the file name to 'http://localhost/{virtual_root}/". The below code snippet gives us an idea of how this can be done.

Note: Virtual folder path can be different from actual physical path of the root folder of an application.

string url = Request.RawUrl;
//Get the virtual root directory.
int index = url.LastIndexOf('/');
string virtualDirectory = url.Substring(0,index+1);
Response.Write("Virtual path is >> " + url);

Once we get the virtual path, then we construct the URL for each request as follows:

string uri = "http://localhost" + {virtualDirectory} + 
               {aspx file name} + "?PreCompile=true"

You must have observed that we are appending a HTTP request parameter (PreCompile=true) to the URL. We are doing so because on the server side, we shall check for this special request parameter. And if the request contains this special parameter, then we stop all processing and just return. This would prevent further unnecessary code processing of all ASPX pages, as we just want the pages to be compiled. And the best place to handle this is in the Application_PreRequestHandlerExecute event handler of Global.asax. The code snippet below shows how we can do this in Global.asax:

protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
    string preCompile = Request["PreCompile"];
    if(preCompile != null)//If request is for Pre-Compile, just return..

Using the code

To use the tool, just follow the steps below for your web application:

  1. Copy PreCompile.aspx and PreCompile.aspx.cs to the root directory of your application.
  2. Add the Application_PreRequestHandlerExecute event handler to your application's Global.asax file.
  3. Simply make a request to the PreCompile.aspx page from your web browser.

Points of Interest

This tool can be used to pre-compile any application, by just placing PreCompile.aspx and the code-behind file in the root folder of the web application.

In future releases of .NET framework, Microsoft is planning to provide developers with a similar tool to pre-compile aspx pages.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Narendra Naidu
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralAnother way to do the same Pin
pekadorweb29-May-07 6:04
memberpekadorweb29-May-07 6:04 
GeneralThe third step. Pin
jhtang11-Jan-06 3:36
memberjhtang11-Jan-06 3:36 
Questiondoes this solution actually do anything? Pin
TimRobinson3316-Aug-05 6:52
memberTimRobinson3316-Aug-05 6:52 
AnswerRe: does this solution actually do anything? Pin
TimRobinson3322-Aug-05 1:36
memberTimRobinson3322-Aug-05 1:36 
GeneralRunning this on forms authenticated folders Pin
windust4-Jan-05 10:56
memberwindust4-Jan-05 10:56 
GeneralTrapping Compilation Errors Pin
Anonymous2-Jul-04 8:28
memberAnonymous2-Jul-04 8:28 
GeneralWhy not just be hardcore and ... Pin
leppie21-Jan-04 6:15
memberleppie21-Jan-04 6:15 
GeneralRe: Why not just be hardcore and ... Pin
kaschimer21-Jan-04 8:03
memberkaschimer21-Jan-04 8:03 
GeneralRe: Why not just be hardcore and ... Pin
partyganger21-Jan-04 8:22
memberpartyganger21-Jan-04 8:22 
GeneralRe: Why not just be hardcore and ... Pin
Anonymous1-Apr-04 3:43
memberAnonymous1-Apr-04 3:43 
GeneralRe: Why not just be hardcore and ... Pin
partyganger1-Apr-04 4:10
memberpartyganger1-Apr-04 4:10 
GeneralOther solutions - exisiting for a long time Pin
Anonymous21-Jan-04 3:46
memberAnonymous21-Jan-04 3:46 
GeneralRe: Other solutions - exisiting for a long time Pin
Sarvesvara (BVKS) Dasa21-Jan-04 5:44
memberSarvesvara (BVKS) Dasa21-Jan-04 5:44 
GeneralRe: Other solutions - exisiting for a long time Pin
Anonymous19-May-04 1:41
memberAnonymous19-May-04 1:41 
Generalbut this has a new idea Pin
Michael Freidgeim9-Nov-04 15:54
memberMichael Freidgeim9-Nov-04 15:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190306.1 | Last Updated 31 Mar 2004
Article Copyright 2004 by Narendra Naidu
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid