Click here to Skip to main content
15,867,594 members
Articles / Web Development / ASP.NET
Article

Yet another pre-compile tool for ASP.NET web sites

Rate me:
Please Sign up or sign in to vote.
4.10/5 (9 votes)
24 May 20045 min read 100.5K   744   36   24
This article describes a VB.NET tool that make easier to pre-compile an ASP.NET site.

Introduction

I know: the problem of pre-compiling an ASP.NET web site in order to avoid the classic "compilation delay" felt by the first user browsing those site's pages has been faced many times, and a lot of solutions have been proposed to solve it. But, when I was looking for these solutions to a classic problem, I didn't find the one that I liked completely. So, I decided to develop my own ASP.NET precompiler.

Of course, now you are thinking: "Uff, yet another pre-compile tool for ASP.NET web sites". And you're so right that I decided to title my article exactly with the words you have in your mind. But... before you stop reading, take a look at some peculiarities of the tool I propose.

Background

My starting point was the CodeProject article "Pre-Compile ASPX pages in .NET 1.1" by Narendra Naidu: there, the precompiling task was faced by simply adding to the web site a special ASP.NET page simulating an HttpWebRequest towards each ASPX page of the web site itself.

That was a nice and simple solution, but not suitable in my specific situation. In fact, I was looking for:

  • something not implemented as a part of the web site, but as a separate application;
  • something not forcing me to precompile all the pages of my web site, but capable to focus only on a specific subset of them (for example, the most frequently used ones);
  • something working also with web sites protected by the ASP.NET Web Form Authentication mechanism;
  • something able to simulate client web requests coming from a specific browser;
  • something with the ability to keep track of compilation errors.

The ASP.NET precompiler I wrote matches all these goals, in fact:

  • it's a Windows Forms application, that doesn't force you to include the precompiler logic inside the web site itself;
  • it doesn't retrieve the pages to visit recursively walking on the web application folders tree, but it takes them from a user-configurable list of URLs;
  • it works also with web sites protected by Web Form Authentication, supporting a specific POST of data to the Login page and correctly managing the client-side authentication cookie;
  • it can "impersonate" a specific user agent, allowing you to work also with sites where some browser checks are implemented to force a navigation restriction;
  • it logs any error occurring during HTTP requests, that is very useful if - for some reason (for example, to have a more flexible deploying strategy) - you are not compiling your site's code-behind pages into a DLL assembly (that is: you're taking advantage of the src attribute of the @Page directive).

How to use the tool

As stated before, the tool forces the compilation of ASPX files just invoking their URLs in a web request. The list of the URLs to be hit is stored in a user-editable text file, that we'll simply call "URL file". The format of the URL file is simple: it stores an URL on each line, separating with a TAB character the base URL part from the relative path of the ASPX page. This split URL paths make simpler to invoke the same set of pages on different web sites (this is useful, for example, if you have various copies of the same ASP.NET site on different environments).

To create the URL file, you can use ASP.NET precompiler itself: select the "Generate URLs" option from the "File" menu and specify: the physical location of your web site's files, the base URL that will precede the relative page path in the final URL, and the filename for your new URL file.

Generating URLs

ASP.NET precompiler will walk recursively the specified path looking for ASPX files and it will generate your URL file, that can be manually edited later (for example, to refine or limit the set of pages to be precompiled).

To make the ASP.NET precompiler less "invasive" on your site, you can adopt a trick I found in Narendra Naidu's article: by adding a Response.End() inside the pre-request event handler of Global.asax, you can abort the page processing and rendering when you call your pages with a special parameter (for example, PreCompile=true). In this way, the requests coming from ASP.NET precompiler will cause the page compilation but not the complete overhead of serving a complete web request.

VB
Private Sub Global_PreRequestHandlerExecute(...) _
                 Handles MyBase.PreRequestHandlerExecute
  ' Prevent processing if page was called with a "PreCompile" parameter
  If Not Request("PreCompile") Is Nothing Then Response.End()
End Sub

If you are planning to use this trick, you'll need to check the "Include PreCompile parameter" option when generating the URLs list with ASP.NET precompiler. Also, keep in mind that the Global_PreRequestHandlerExecute() handler will be invoked for each page requested on your web application, and this might cause a little impact on the web site performance.

When the URL file is ready, before starting the pre-compilation process, you need to configure some parameters on the ASP.NET precompiler's main form:

ASP.NET Precompiler main form

  • specify the name of the URL file you are going to use;
  • if you need to simulate web request from a specific browser, fill the textbox User Agent with the correct user agent string;
  • if you plan to invoke the pages stored in the URL file substituting the original base URL path with another one, use the Alternative Web Root option;
  • if the site you're working on is protected by Web Form Authentication, use the Web Form Authentication option, specifying the relative path for the Login page and the parameters it expects; activating this option will produce a preliminary POST request to the Login page to obtain the authentication cookie for subsequent requests (don't forget to include the __VIEWSTATE parameter with a valid value in the POST params textbox);
  • if you want to log any error occurred during the compilation process, activate the Log errors option, specifying the filename for the log (that is an HTML-formatted file, for easy viewing in a browser window);
  • if you need to clear the error log or the cookies collection before compiling, use the Reset cookies or the Reset error log option respectively.

The configuration settings you just set can be saved to disk for future use. The reuse of ASP.NET Precompiler settings is accomplished by the options of the "File" menu (for the storage mechanism underlying the saving/retrieval of these data, see my article about managing configuration settings persistence).

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


Written By
Technical Lead
Italy Italy
I was born in 1970.

My first computer experience dates back to early 80s, with a Sinclair ZX81.
From that time on, as many "friends" say, my IT-illness has increased year by year.

I graduated in Electronic Engineering and earned the following Microsoft certifications:
MCP, MCT, MCDBA, MCSD, MCAD, MCSD for .NET (early achiever).

I worked in IT as a developer, a teacher, a consultant, a technical writer, a technical leader.
IT knowledge applied to real life is my primary interest and focus.

Comments and Discussions

 
GeneralRun from logon script or batch file Pin
daleallenc4-Aug-08 8:12
daleallenc4-Aug-08 8:12 
AnswerRe: Run from logon script or batch file Pin
Alberto Venditti5-Aug-08 5:09
Alberto Venditti5-Aug-08 5:09 
GeneralRe: Run from logon script or batch file Pin
daleallenc6-Aug-08 3:25
daleallenc6-Aug-08 3:25 
GeneralRe: Run from logon script or batch file [modified] Pin
Alberto Venditti6-Aug-08 3:42
Alberto Venditti6-Aug-08 3:42 
GeneralRe: Run from logon script or batch file Pin
daleallenc7-Aug-08 3:54
daleallenc7-Aug-08 3:54 
GeneralRe: Run from logon script or batch file Pin
Alberto Venditti7-Aug-08 5:36
Alberto Venditti7-Aug-08 5:36 
GeneralRe: Run from logon script or batch file Pin
daleallenc7-Aug-08 8:52
daleallenc7-Aug-08 8:52 
GeneralRe: Run from logon script or batch file Pin
Alberto Venditti7-Aug-08 23:35
Alberto Venditti7-Aug-08 23:35 
GeneralRe: Run from logon script or batch file Pin
daleallenc8-Aug-08 3:12
daleallenc8-Aug-08 3:12 

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.