How to check ASP.NET Core RC2/1.0 Razor views for compile errors?

前端 未结 1 1247
忘了有多久
忘了有多久 2021-02-20 13:54

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 o

1条回答
  •  忘掉有多难
    2021-02-20 14:21

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

    1. Add the correct view compilation package reference to your csproj. In the containing all of your project's tags, add:

    2. Add the MvcRazorCompileOnPublish property to your csproj. In the section containing your project , etc, add:

      true

    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.

    0 讨论(0)
提交回复
热议问题