如何检查ASP.NET Core RC2/1.0 Razor视图是否存在编译错误? [英] How to check ASP.NET Core RC2/1.0 Razor views for compile errors?

查看:92
本文介绍了如何检查ASP.NET Core RC2/1.0 Razor视图是否存在编译错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Razor视图预编译已从RC2中删除,原因是 ASP.NET CLI示例在HelloMvc项目上对此进行测试.Views\Home\Index.cshtml文件可以里面几乎没有任何内容,dotnet build仍然会成功.)

解决方案

当前的RC2构建系统似乎完全忽略了Compiler/Preprocess文件夹-您可以直接在其中放置任何内容,并且不会产生任何构建错误.直到Roslyn连接回去进行预编译之前,我认为目前无法在构建阶段检查.cshtml文件.

我发现的唯一解决方法是让Visual Studio使用一些肮脏的精细/替换技巧打开项目中的每个 .cshtml文件,并让Intellisense引擎检查剃刀代码./p>

更新

查看承诺删除剃须刀预编译,整个RazorPreCompileModule本身实际上已被删除,并且一段时间不会回来.即使将这段代码手动添加回您的项目中,dotnet构建也不会运行任何编译模块.

更新2

View编译又回到了ASP.NET Core 1.1中!

要启用它,请将以下内容添加到project.json的"dependencies"部分:

"dependencies": {
    "Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Design": "1.1.0-preview4-final"
}

以及工具"部分的以下内容:

"tools": {
    "Microsoft.AspNetCore.Razor.Tools: 1.1.0-preview4-final",
    "Microsoft.AspNetCore.Mvc.Razor.ViewCompilation.Tools": "1.1.0-preview4-final"
}

,然后在脚本"下,将razor-precompile命令添加到发布后":

"scripts": {
    "postpublish": [
        "dotnet razor-precompile -configuration %publish:Configuration% -framework %publish:TargetFramework% -output-path %publish:OutputPath% %publish:ProjectPath%"
    ]
}

更新3-csproj

我们终于走到了VS2017并执行了向csproj的项目迁移.当然,这破坏了剃须刀的预编译,男孩还真是想办法解决它的麻烦之处-添加到.csproj中来运行发布后脚本,但是现在无论如何剃须刀预编译都不需要这样做.因此,事不宜迟,在迁移项目后,以下是在VS2017/csproj land中启用剃须刀预编译的方法:

  1. 向您的csproj添加正确的视图编译包引用.在包含所有项目的<PackageReference>标记的<ItemGroup>中,添加:

    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="1.1.0" />

  2. 将MvcRazorCompileOnPublish属性添加到csproj.在包含项目<VersionPrefix><TargetFramework>等的<PropertyGroup>部分中,添加:

    <MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>

您已经完成.现在,每次发布项目时,构建系统都将运行razor预编译.发布时,您应该在构建输出中看到类似Razor view compilation for myApp -> obj\Release\netcoreapp1.1\myApp.PrecompiledViews.dll的内容.

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:

  1. 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" />

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

这篇关于如何检查ASP.NET Core RC2/1.0 Razor视图是否存在编译错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆