Search code examples
asp.netrazorrazor-2precompilepre-compilation

Trouble Precompiling Views After Updating to .NET Framework 4.6


We are in the process of upgrading our ASP.Net (MVC 5) project from .NET Framework 4.5.2 to .NET Framework 4.6. We have not updated to MVC 6, yet.

We have the MvcBuildViews project property set to true. This has worked fine in the past, but after changing the project's Target Framework to ".NET Framework 4.6," we get the following error at build time:

error MSB6004: The specified task executable location "C:\windows\Microsoft.NET\Framework64\aspnet_compiler.exe" is invalid.

This also occurs when invoking MSBuild.exe directly with the following command line:

C:\Program Files (x86)\MSBuild\14.0\bin\msbuild.exe .\Estream.sln /m /nologo /p:Configuration=Debug;Platform=Mixed Platforms /property:MvcBuildViews=true /t:build /v:m

Has anybody else seen a similar problem? What do I need to do differently using .NET Framework 4.6 in order to precompile views?

I am aware of the ICompileModule and RazorPreCompileModule as mentioned here, but we are not using MVC 6.

Any help would be greatly appreciated.


Solution

  • More than likely you don't have the TargetFramework set. Your first red flag would be the path. Notice:

    C:\windows\Microsoft.NET\Framework64\aspnet_compiler.exe

    If you look in your Project file (.proj), you will find how that path is generated; which would look something like this:

    $(windir)\Microsoft.NET\Framework64\$(TargetFrameworkVersionNumber)

    Odd ... it would appear we are missing the TargetFrameworkVersionNumber variable? Where's that going?

    Now if you look how your TargetFrameworkVersionNumber variable is being set, you will notice that you may be missing a targeted framework:

    In Visual Studio 2015 your target framework is most likely 4.6.1. So, simply adding in the code:

    <PropertyGroup Condition=" '$(TargetFrameworkVersion)' == 'v4.6.1' ">
      <TargetFrameworkVersionNumber>v4.0.30319</TargetFrameworkVersionNumber>
    </PropertyGroup>
    

    should, in theory, resolve your problem. At least, it did for our team.

    Hope that helps!