8

By default, Visual Studio only checks for build errors on CSHTML files if they are open when you build. I want to make that happen at build for closed CSHTML files as well.

I have seen in this post: How to compile cshtml before runtime or this post

The solution was simple, just to set this in the csproj file:

<MvcBuildViews>true</MvcBuildViews>

Unfortunately this starts raising errors in cs files that it generates in temp folders. The cs files are the converted cs html files, and by themselves are not compile-able, hence I get a variety of build errors that don't actually exist.


My current solution is that I keep the MvcBuildViews setting false, and then manually open all the CSHTML files in the project before building.

Is there a fix for skipping the temp files, or a more efficient way than opening all the CSHTML files?

Community
  • 1
  • 1

1 Answers1

2

Is there a fix for skipping the temp files, or a more efficient way than opening all the CSHTML files?

The only supported way is the <MvcBuildViews> project configuration element. It is support by MsBuild which allows unattened builds to work. It is the most efficient way to check for build errors in views.

The cs files are the converted cs html files

What does that mean? Views are vbhtml or cshtml. There are no views with the .cs extension.

Without adding the correct configuration elements (below) you can get some odd errors when the cshtml files are converted to .cs (intermediate files) then compiled.

Unfortunately this starts raising errors in cs files that it generates in temp folders.

There are an abundance of answers (1 2 3 4) to using the <MvcBuildViews>.

The important thing to understand about MVC projects (actually web projects in general) is that each of the views are compiled by default in the obj/bin directory of the project. The problem typically exists that when IIS and IIS Express try to compile the views again, when they already exist. Thus, the fixes listed above change the compile directory to a directory outside the project.

Community
  • 1
  • 1
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • Views are converted into .CS files. Then they are built. This is how MVC keeps a good speed. This all happens in temp folders that are not part of the project. The problem is that on my machine, with the set to true, that it starts checking these temp files as well. Since the temp files are disconnected, class references don't work in it and throw errors. –  Jun 16 '14 at 12:59
  • Yes the temp folders are not in the project (.csproj), but by default the temp folder used is `obj` in the project folder (where ever the .csproj file is located). [As explained in those abudnance of answers is to add an attribute `` with a path that is not in the project folder](http://stackoverflow.com/a/2862996/209259). – Erik Philips Jun 19 '14 at 16:17