Pre-compilation and code-hiding in ASP.Net

18 October, 2009 | | 0 comments |

Share |
ASP.Net fanatics here comes something to relieve you from the pain and frustration. Do you always wanted to get your ASP.Net application/web-site loaded faster or you always wanted to hide your code from your not-so-very-trusted-client or say both ? Tough questions have the simplest of the answers and I'm not going to contradict that proverb by any chance. But before I dive into the question just let me clear some of the much needed basics. So just read on....

The very first time a request is sent for an .aspx page the .Net Framework residing on the computer which physically hosts these files compiles it and generates the corresponding .dll files i.e. assemblies which are the results of JIT(just-in-time) compilation.

In other words, the .aspx page has been serialized in the form of a .dll file. These .dll files are used in subsequent requests by de-serializing them to render the HTML to the user by the .Net Framework.

So the real catch is whether you can avoid the initial compilation and if yes the code i.e. your .aspx page can be hidden or not ?

Now assuming that you have Visual Studio 2005 or higher and IIS configured with your .Net Framework you can follow the below mentioned steps in Command Prompt to get the pre-compilation done :

1] You have to navigate to the right destination of your .Net Framework 2.0 :

cd "C:\Windows\Microsoft.NET\Framework\v2.0.50727"

2] You have to then compile your project/web-site folder with the following command :

aspnet_compiler -v /GuruSample -p "C:\GuruSample" "c:\Output"

-v /GuruSample --> The virtual path to the project/web-site
-p "src" "target" --> The absolute physical path to the project folder and the location of the target files


Giving the "-p" switch means the IIS metabase will *not* be referenced during pre-compilation. Also it means the target path is mandatory.

Once the above command is executed the following files and folders will be generated at the target location :

  • PreCompiledApp.config
  • Web.Config
  • GuruSample.sln
  • Default.aspx
  • A "bin" folder will also be generated which will be having the pre-compiled .dll file(s).

Your process of pre-compilation is done and now you can safely copy the target files and folders to the "wwwroot" folder of your IIS directory if you're currently experimenting it in your own PC. You can view the .aspx page using the "http://localhost/Default.aspx" switch in your browser.


1] Any changes made to the above files or folders needs re-compilation with all the above mentioned steps.

2] The .aspx file after pre-compilation need not be present. This helps in code-hiding.

3] You can download my "sample project folder" as used in the above explanation Here.