Tuesday, 13 January 2015

Precompiling ASP.NET MVC views in Azure Cloud Service - RazorGenerator

Problem -

Recently, we moved our application from Azure Website to Cloud Service. In website, we were using pre-compilation as that gives a significant performance improvement. When we moved to cloud service and wanted to use pre-compilation, we found that this is not as easy as it was for website. For websites, we are having option in Visual Studio (a check box that you can check) to enable pre-compilation, but, Visual Studio does not provide any such option.

Solution -

One very easy and effective solution for this is to use RazorGenerator – a visual studio extension and a Nuget package (you need both).

Steps to implement -

Below are the steps to implement RazorGenerator for your ASP.NET MVC application -
  1. Install RazorGenerator from Visual Studio extension gallery.
  2. Install package RazorGenerator.mvc from Nuget package to your class library.
  3. Go to view > properties (you can get this by right clicking on the view file in solution explorer or pressing Alt + Enter).
  4. Set Custom Tool to RazorGenerator (you need to do the same for all existing views those you want to be precompiled).
  5. Save the property and you are done.



How this works -

When you set Custom tool to RazorGenerator, this will create a precompiled .cs file named [yourViewName].generated.cs, and the .cs file will be used when the application is running.





No comments:

Post a Comment