Today I enabled compilation of .aspx and .cshtml files using MvcBuildViews parameter as described in this post.
This immediately caught a bunch of problems in my MVC project. Here’s a list of gotchas for myself to remember:
1. In Razor, implicit typing (aka the var) is only supported in full blocks. For example the following will not work:
@var i = ViewData["counter"];
while the following will work Ok:
@{ var i = ViewData["counter"]; }
2. When you use Resharper’s refactoring features to rename namespaces or classes, the changes do not propagate to Razor files. This is because Resharper’s support of Razor is very limited. They have promised to fix that in RS 6.0 but for now we have to remember to manually update them.
3. When you delete files or folders from your Visual Studio solution make sure the changes are properly check-in to the source control. Sometimes a file or folder is not visible in solution explorer but is still sitting on the disk, or even deleted from your local disk but not from the source control, naturally this messes up the aspnet_compiler.exe
4. If you want to deploy your MvcBuildViews-enabled project using the DeployOnBuild method (i.e. in one step with compilation) you have to know that the two features are really in conflict with each other. The MSDeployPublish runs first, generating the package folder, and MvcBuildViews runs second, stumbling upon the second Web.Config (the one in the package folder). One way to solve it is modify the .csproj file to make MvcBuildViews run before MSDeployPublish if DeployOnBuild is enabled or after the AfterBuild if DeployOnBuild is disabled:
<Target Name="MvcBuildViews" Condition="'$(MvcBuildViews)'=='true'">
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(WebProjectOutputDir)" />
</Target>
<PropertyGroup>
<MSDeployPublishDependsOn Condition="'$(DeployOnBuild)|$(DeployTarget)'=='true|MsDeployPublish'">
MvcBuildViews;
$(MSDeployPublishDependsOn)
</MSDeployPublishDependsOn>
<BuildDependsOn Condition="'$(DeployOnBuild)'!='true'">
$(BuildDependsOn);
MvcBuildViews
</BuildDependsOn>
</PropertyGroup>
