Daily Archives: March 18, 2014

Compiling your MVC views without IIS errors

Visual Studio and ASP.NET by default does not compile your MVC views during build, instead the runtime processes them at run time. The consequence of this is that any error in your views may break your app only at run time.

Fortunately you can force Visual Studio to compile your MVC views as part of the build process. Just open your .csproj file and switch the value of the MvcBuildViews element from false to true:


This fixes the original problem, however it often introduces another issue. You may receive the following error when you try to build or publish your web application:

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.

I don’t know who made up this message and what was his intention to communicate. This message does not help neither to find the error, nor to fix it.

The solution – which is definitely not obvious from the previous error – is to delete the obj folder before build.

If you do not want to manually delete that folder every time, you can add that step into your build process. Thankfully MSBuild provides the BaseIntermediateOutputPath variable which points right to the obj folder, and you can combine it with the RemoveDir task to get rid of that folder. Just open the .csproj file again and add a new element to the BeforeBuild target:

<Target Name="BeforeBuild">
    <RemoveDir Directories="$(BaseIntermediateOutputPath)" />

If you don’t like the bin folder and want to remove that as well, you can refer to it via the BaseOutputPath variable.


Technorati-címkék: ,,