Notes on Building Razor Views

Wednesday, February 16, 2011

Just like web form views, razor views don’t “build” when you build an MVC project in Visual Studio. By default, the parsing and compilation of a view doesn’t happen until runtime, meaning you might not know about a syntax error in a view until you hit the application with a web browser.

Since it’s first release, ASP.NET MVC has provided the ability to parse and compile views at build time by manipulating the underlying MSBuild (.csproj) file. The option works for both web forms (.aspx) and Razor (.cshtml) views.

1. Right-click the MVC project and select “Unload”

2. Right-click the unloaded project and select “Edit”

3. Locate the <MvcBuildViews> element and set its value to “true”.

4. Save the file, then right-click the unloaded project and reload.

With MvcBuildViews set to true, you’ll find out about any syntax problems when you compile a project. Also, Al Gonzalez has a Visual Studio macro to turn the above steps into a one-click operation.

Building views will significantly increase your overall build time, so you might consider only setting the option to true for release builds. 

<PropertyGroup 
  Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
  <MvcBuildViews>true</MvcBuildViews>

</PropertyGroup>

What’s Up With .Edmx Files?

If you happen to have an Entity Framework edmx file directly inside your MVC project, you might see the following error when you eagerly build views.

Could not load type 'System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider'.

This error message is easy to avoid because we do not need to process the .edmx file with a build provider in MVC. Simply add the following configuration into the root level web.config, and all will be well.

<system.web>               
  <compilation debug="true" targetFramework="4.0">
    <buildProviders>
      <remove extension=".edmx"/>
    </buildProviders>
    ...

Happy building!


Comments
gravatar Eric Appel Wednesday, February 16, 2011
Does building views during compilation result in better startup performance for the site?
gravatar Eric Appel Wednesday, February 16, 2011
Actually, my question should read "does compiling views during the build result in better performance?"
gravatar scott Wednesday, February 16, 2011
There should be a slight improvement in startup time (time to render a view the first time, or a directory of views), but you'd have to take some measurements to see exactly the type of improvement on a specific project. It's generally marginal.
Ryan W Wednesday, February 16, 2011
Good tip. I'm trying it out with a WebForms View Engine based project and getting this error (VS2010):

"It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS."

It's pointing to the root level web.config in the list of errors, but I need those elements its complaining about (authentication, etc) still. Maybe you've seen this?
gravatar scott Wednesday, February 16, 2011
@Ryan: I have seen that error when I'm trying to compile the wrong folder. I wonder how that could happen in VS, I've only seen it when using the command line aspnet_compiler.
gravatar Ryan W Thursday, February 17, 2011
Found the answer on SO, had to delete the bin and obj directories, then it worked.

stackoverflow.com/...

Thanks!
gravatar Thanigainathan Saturday, February 19, 2011
Hi,
I tried this build view options .I can see the site performance seems good after this. But as you say this should be enabled for only release mode.

Thanks,
Thani
gravatar Petr Lev Monday, February 21, 2011
I am going to try measure the final performance.
Comments are now closed.
by K. Scott Allen K.Scott Allen
My Pluralsight Courses
The Podcast!