Precompiling ASP.NET websites with Visual Studio 2010

Precompiling ASP.NET 2.0 websites
ASP.NET 2.0 provides the ability to precompile websites into binaries before deploying them to the server. This has been a desired feature in the past and in this article we will see how simple it is to do so using ASP.NET 2.0.
When a page is first requested from the server, the page is compiled by ASP.NET and then served. Even though one may argue that it does not take a long time for the compilation process to execute, however for sites which are updated frequently, it could lead to some first-time overheads issues.
ASP.NET 2.0 introduces the option of precompiling your site. This offers the following advantages :
1.    You do not need to deploy the source code to the server. The website is precompiled into binaries, which is then deployed to the server.
2.    It allows you to identify bugs first hand, during compilation.
3.    Makes the deployment process a lot easier as all your aspx pages, user controls etc. are compiled into binaries.
There are two ways in which we can precompile a website.
Precompiling websites through command-line
To precompile a website using the command line, follow these steps :
1.    Navigate to the directory C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727.
2.    Run the aspnet_compiler command in the command prompt by typing:
aspnet_compiler -p <physicalpath> -v / <targetPath>
For e.g. let us assume that your website ‘Sampleweb1’ is stored at ‘C:\Websites’. To compile this website, you will type the following at the command prompt :
aspnet_compiler – p C:\Websites\SampleWeb1 –v /MyFirstWebsite C:\Projects\Samples
where :
·         C:\Websites\SampleWeb1 – is the physical path to your website
·         /MyFirstWebsite – is the virtual directory used at runtime
·         C:\Projects\Samples – is the destination directory.
3.    After the command has been run, open the C:\Projects\Samples directory to find a bin directory with the .dll and .compiled files.
Note: All your images, .css, .htm etc are not compiled into binaries. They are just copied to the target folder.
Precompile and publish websites using Visual Studio 2005
To precompile and publish your website using Visual Studio, follow these steps:
1.    Open your website using Visual Studio.
2.    Go to the Build Menu > Publish Web Site.
3.    You get a Publish Web Site dialog where you can specify the path to publish to. Clicking on the eplisis(…) gives you the option of choosing your website output to  File System, IIS, FTP or a remote site.
4.     Select your options in the dialog :
a.    Allow this precompiled site to be updatable – This option allows you to change the markup and client side functionality of the .aspx pages. However you cannot access the server side code.
b.    Use Fixed Naming And Single Page Assemblies – This option is not available for in place compilation. It turns off batch builds during precompilation to generate dll’s with fixed names.
c.    Enable strong naming on precompiled assemblies – This option specifies that your assemblies are strong named using a key file or container.
5.    Click Ok to compile and publish the website.
 Conclusion
Well that was pretty easy huh! I see this precompilation feature to be more advantageous from a security perspective rather than the slight performance benefit it offers. The security perspective is that the precompilation feature protects your source code on the server and helps you identify compilation bugs, early hand.
Advertisements
By simplemsexchange Posted in ASP.NET

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s