53

I've recently installed VS 2012 Professional.

When i try to build my MVC4 Web Project. It doesn't recognize error in razor view when i do "Build" or "Re-Build".

Example :
I removed a namespace from the project / or say renamed it. i build the solution, it gave me errors in all cs files, which i fixed by changing the namespace. The entire solution build successfully. When i run the project it gave me Compilation Error saying that namespace not found, because the old namespace was still referred in some of the views (*.cshtml files).

Expected Solution :
I wish when i do "Build" or "Re-Build", it should recognize such errors and show me along with any other errors.

This was working fine with VS 2010, am i missing any configuration?

Thanks In Advance !! Amit

Edit I found the answer myself, i think it was early to post the question :

razor syntax with errors compiles when it should not compile

Another Problem

After changing value to True in .csproject file, when i start building the project it shows error, but it shows only one error at a time. let's say, i've 5 errors in total 3 views. it would just show me one error. Is there any solution so that it shows all the 5 errors ?

Community
  • 1
  • 1
Amit Andharia
  • 909
  • 1
  • 8
  • 16

5 Answers5

75

When i try to build my MVC4 Web Project. It doesn't recognize error in razor view when i do "Build" or "Re-Build".

Seems normal. Razor views are dynamically compiled by the ASP.NET runtime. If you want your views to be built at compile-time you could add the following option to your .csproj file:

<PropertyGroup>
    <MvcBuildViews>true</MvcBuildViews>
</PropertyGroup>

You may take a look at the this article for more details.

Ruchan
  • 3,124
  • 7
  • 36
  • 72
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Usefull information. Thx – Maris Apr 17 '13 at 06:43
  • @Darin Dimitrov this will make execution faster? at least the first time? – elranu May 20 '13 at 23:55
  • @elranu, yeah the first request after the application pool has been restarted might be very slightly faster but the difference might not even be noticeable. The real benefit of precompiling the Razor views is not performance but the compile-time safety that you get during development. – Darin Dimitrov May 21 '13 at 06:02
  • 2
    I don't recommend using this tag in debug mode because every time you run the app, the views have to be compiled (making it slower) and you cannot make a change in a view unless you stop the app. Take a look at my answer to see how you can get compile-time check in views and at the same time be able to edit views. http://stackoverflow.com/a/24665580/754049 – Francisco Goldenstein Jul 10 '14 at 00:07
  • From the linked thread, by @buffjape: "With MvcBuildViews = true, you can still edit views and press F5 to see changes immediately." – Korijn Jan 20 '15 at 08:55
21

I recommend you to add MmvcBuildViews tag as a child of the Release PropertyGroup tag so the views are only compiled when you compile in Release mode (or when you publish in Release mode). This way, your app is faster when you are debugging. And, you get compile-time checks before deploying (when you build on Release mode). In summary, you get the best of both worlds.

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <MvcBuildViews>true</MvcBuildViews>
  </PropertyGroup>
Francisco Goldenstein
  • 13,299
  • 7
  • 58
  • 74
18

According to my experience, besides the true setting mentioned above, you still need to ensure below setting exist in your csproj file:

<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>
karl li
  • 1,316
  • 15
  • 19
  • for me, this was complete answer. thanks – Omid.Hanjani Jul 30 '16 at 08:30
  • AspNetCompiler MSBuild can do AfterBuild to the bin folder. Local build bin folder is in source tree. TFS Teambuild compiles to a different dir on build server. AspNetCompiler task needs to find bin directory to ref the required DLL. Solution is to modify the AfterBuild target of the MVC Project needs 2 entries Then you compile at local as well as TFS. – ransems Mar 02 '22 at 13:37
  • Also added: true – ransems Mar 02 '22 at 13:38
15

By default it does not compile views. You can enable this feature, but keep in mind that it will increase build time.

You can enable compiling view by following these steps:

  • Unload project
  • Open project file
  • Find <MvcBuildViews>false</MvcBuildViews> and change it to have true
  • Close project file and reload project
Majid Basirati
  • 2,665
  • 3
  • 24
  • 46
Dmitry Efimenko
  • 10,973
  • 7
  • 62
  • 79
1

Its really Very Simple Answer Go through this Steps:

Unload Project

Edit Project

Than Search :

<Target Name="MvcBuildViews" AfterTargets="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
  <AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>

below add

<Target Name="AfterBuild" Condition="'$(Configuration)'!='Debug'">
  <RemoveDir Directories="$(BaseIntermediateOutputPath)" />
</Target>

That's it Than Unload Project . Build

And Check Ur Syntax Is work properly .

I am Sure.

cakan
  • 2,099
  • 5
  • 32
  • 42
vishal joshi
  • 77
  • 1
  • 2