Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force compilation of ASP.NET MVC views?

I have a Windows Azure web role that contains a web site using ASP.NET MVC. When an HTTP request arrives and a page is first loaded the view (.aspx or .cshtml) is compiled and that takes some time and so the first time a page is served it takes notable longer than later serving the same page.

I've enabled <MvcBuildViews> (described in this answer) to enforce compile-time validation of views, but that doesn't seem to have any effect on their compilation when the site is deployed and running.

Azure web roles have so-called startup tasks and also a special OnStart() method where I can place whatever warmup code, so once I know what to do adding that into the role is not a problem.

Is there a way to force compilation of all views?

like image 400
sharptooth Avatar asked May 16 '12 12:05

sharptooth


People also ask

Do Cshtml files get compiled?

Razor files with a . cshtml extension are compiled at both build and publish time using the Razor SDK. Runtime compilation may be optionally enabled by configuring your project.

How do I enable razor runtime compilation?

You just need to check “Enable Razor runtime compilation” when you're creating a new project. Moreover, this option is available when choosing the target framework starting from version 3.1 to the current version 5.

What is MvcBuildViews?

MvcBuildViews builds views temporarily and gives build results back to building process. If there were errors then build fails and errors are shown in Visual Studio errors window.

How many views does the model can have in MVC?

ASP.NET MVC view can't have more than one model so if we need to display properties from more than one model in the view, it is not possible. ViewModel serves this purpose.


1 Answers

Take a look at Precompiled Razor Views by David Ebbo

Why would you want to do that?

One reason to do this is to avoid any runtime hit when your site starts, since there is nothing left to compile at runtime. This can be significant in sites with many views.

Also, you no longer need to deploy the cshtml files at all, resulting in a smaller deployment file set.

Another cool benefit is that it gives you the ability to unit test your views, which has always been something very difficult with the standard runtime compilation model. I’ll cover that in more details in a future post.

like image 129
archil Avatar answered Sep 22 '22 01:09

archil