Click here to Skip to main content
Click here to Skip to main content

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.

Introduction

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.

Background

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 = 
  (HttpWebRequest)WebRequest.Create("http://localhost/naren/test.aspx"); 
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..
    {
        Response.End();
        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.

License

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

Share

About the Author

Narendra Naidu
Architect
India India
No Biography provided

Comments and Discussions

 
GeneralAnother way to do the same Pinmemberpekadorweb29-May-07 7:04 
GeneralThe third step. Pinmemberjhtang11-Jan-06 4:36 
Questiondoes this solution actually do anything? PinmemberTimRobinson3316-Aug-05 7:52 
AnswerRe: does this solution actually do anything? PinmemberTimRobinson3322-Aug-05 2:36 
GeneralRunning this on forms authenticated folders Pinmemberwindust4-Jan-05 11:56 
GeneralTrapping Compilation Errors PinsussAnonymous2-Jul-04 9:28 
GeneralWhy not just be hardcore and ... Pinmemberleppie21-Jan-04 7:15 
GeneralRe: Why not just be hardcore and ... PinmemberRabidKangaroo21-Jan-04 9:03 
GeneralRe: Why not just be hardcore and ... Pinmemberpartyganger21-Jan-04 9:22 
GeneralRe: Why not just be hardcore and ... PinsussAnonymous1-Apr-04 4:43 
GeneralRe: Why not just be hardcore and ... Pinmemberpartyganger1-Apr-04 5:10 
GeneralOther solutions - exisiting for a long time PinsussAnonymous21-Jan-04 4:46 
GeneralRe: Other solutions - exisiting for a long time PinmemberSPS21-Jan-04 6:44 
GeneralRe: Other solutions - exisiting for a long time PinsussAnonymous19-May-04 2:41 
Generalbut this has a new idea PinmemberMichael Freidgeim9-Nov-04 16:54 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 31 Mar 2004
Article Copyright 2004 by Narendra Naidu
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid