Razor view precompilation has been removed from RC2 due to issues with getting it to work in .NET Core.
Is there a way to fail a CI build if there's a syntax error in one of the .cshtml files, or will this not be possible until precompilation is back?
(I'm testing this on the HelloMvc project from ASP.NET CLI samples. The Views\Home\Index.cshtml
file can have literally anything in it and dotnet build
will still succeed.)
The current RC2 build system seems to completely ignore the Compiler/Preprocess
folder - you can literally put anything in it and it will generate no build errors. Until Roslyn is wired back up to do precompilation, I don't think it's currently possible to check .cshtml files in the build stage.
The only workaround I've found is to get Visual Studio to open every .cshtml file in the project using some dirty fine/replace tricks, and have the Intellisense engine check the razor code.
Update
Looking at the commit that removed razor precompilation, it seems that the entire RazorPreCompileModule
itself was actually removed, and won't be back for some time. Even if this code was added back into your project manually, it doesn't look like dotnet build will run any compile modules.
Update 2
View Compilation is back in ASP.NET Core 1.1!
To enable it, add the following into the project.json "dependencies" section:
"dependencies": {
"Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Design": "1.1.0-preview4-final"
}
and the following to your "tools" section:
"tools": {
"Microsoft.AspNetCore.Razor.Tools”: “1.1.0-preview4-final",
"Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools": "1.1.0-preview4-final"
}
and under "scripts", add the razor-precompile command to "postpublish":
"scripts": {
"postpublish": [
"dotnet razor-precompile -configuration %publish:Configuration% -framework %publish:TargetFramework% -output-path %publish:OutputPath% %publish:ProjectPath%"
]
}
Update 3 - csproj
We finally got around to moving to VS2017 and performing the project migration to csproj. This of course broke razor precompilation, and boy was it a rabbit hole to figure out how to fix it - the official instructions are here.
The first hiccup you'll probably hit is the automated xproj/project.json -> csproj migration failing. The automated migration will fail if you have a postpublish
script section in your project.json
, so go ahead and completely delete that section before you do the migration.
As it turns out, you can still run post publish scripts by adding <Target Name="PostPublishTarget" AfterTargets="Publish">...</Target>
to your .csproj
, but this is now unnecessary for razor precompilation anyway. So without further ado, here's how to enable razor precompilation in VS2017/csproj land, once you've migrated your project:
Add the correct view compilation package reference to your csproj. In the <ItemGroup>
containing all of your project's <PackageReference>
tags, add:
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="1.1.0" />
Add the MvcRazorCompileOnPublish property to your csproj. In the <PropertyGroup>
section containing your project <VersionPrefix>
, <TargetFramework>
etc, add:
<MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
And you're done. The build system will now run razor precompilation every time the project is published. You should see something like Razor view compilation for myApp -> obj\Release\netcoreapp1.1\myApp.PrecompiledViews.dll
in your build output when publishing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With