razor views are not giving compile time error

asp.net Mvcasp.net Mvc-4RazorVisual Studio-2012

asp.net Mvc Problem Overview


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 :

https://stackoverflow.com/questions/5014317/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 ?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

> 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.

Solution 2 - asp.net Mvc

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>

Solution 3 - asp.net Mvc

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>

Solution 4 - asp.net Mvc

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

Solution 5 - asp.net Mvc

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAmit AndhariaView Question on Stackoverflow
Solution 1 - asp.net MvcDarin DimitrovView Answer on Stackoverflow
Solution 2 - asp.net MvcFrancisco GoldensteinView Answer on Stackoverflow
Solution 3 - asp.net Mvckarl liView Answer on Stackoverflow
Solution 4 - asp.net MvcDmitry EfimenkoView Answer on Stackoverflow
Solution 5 - asp.net Mvcvishal joshiView Answer on Stackoverflow