Sebastian K
Sebastian K

Reputation: 6403

Is compling Views in ASP.NET MVC good practice?

In ASP.NET MVC, you can enable compilation of views by setting following key in your project file to true:

<MvcBuildViews>true</MvcBuildViews>

Then whenever you compile your solution, your views will be compiled as well, which is great, as it allows you to find errors at compile time rather than run-time. This is particularly useful when refactoring, as it is possible to miss renamed property, or changed namespace in a view.

I can see only benefits, so why this options is not only set by default, but not even available in Visual Studio 2010 UI, and often requiring some additional changes to your config, as mentioned here: http://odetocode.com/blogs/scott/archive/2011/02/16/notes-on-building-razor-views.aspx

Are there any reasonable drawbacks to compiling ASP.NET MVC views?

Upvotes: 3

Views: 535

Answers (2)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93444

I wouldn't say it's "good practice", nor is it "bad practice". It's just another tool in your toolbox.

The reason it's not enabled by default is because, while small projects are ok.. it quickly becomes extremely slow, taking several minutes on medium to larger projects.

Also, it only catches specific kinds of errors. So don't rely on it for everything.

Upvotes: 4

Brian Mains
Brian Mains

Reputation: 50728

Assume this is similar to precompiling pages in a web forms project. Yes there would be much benefit to that as it would make the application perform better, since the view wouldn't have to be compiled when the user accesses the view.

This can make smaller changes much harder though; I've run into scenarios where you have to make a small change to the code. If you had to redeploy, it could recycle the application and force users to lose work. I'll be honest, most of my using this type of feature was with web forms, but the process should be similar.

It also made deployments a little harder, but I don't really count that as a con since deployments are infrequent.

Upvotes: 1

Related Questions