|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThe 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. BackgroundThe 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 myHttpWebRequest = null;
HttpWebResponse myHttpWebResponse = null;
myHttpWebRequest =
(HttpWebRequest)WebRequest.Create("http://localhost/naren/test.aspx");
myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
About the Global.asax fileThe Global.asax file contains global event handlers that get called for important events such as The Pre-Compilation ToolThe 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 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 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 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 codeTo use the tool, just follow the steps below for your web application:
Points of InterestThis 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.
|
||||||||||||||||||||||