我正在 VS2017 中处理 .NET Core 2 MVC 项目。我注意到在使用命令行构建时,我的 Razor View 中的任何 C# 错误都会导致构建失败。但是,在 VS2017 中构建时,构建将运行良好。如果我有一个 .cshtml
打开包含 C# 错误的文件,它们将在错误列表中显示为错误,但它们不会阻止构建完成。
我不确定为什么我会得到两个不同的结果,因为肯定两者都在后台使用 MSBuild?有没有办法让 VS 以与命令行相同的方式在 Razor C# 错误上失败?
最佳答案
使用 ASP.NET Core 2.x Razor,如果 Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
可以在发布时预编译 View 包含包(将 MvcRazorCompileOnPublish
设置为 true 是可选的)。当前无法将 View 编译为标准调试版本的一部分。
更多详情请见 Razor view compilation and precompilation in ASP.NET Core
If your project targets .NET Framework, include a package reference to Microsoft.AspNetCore.Mvc.Razor.ViewCompilation:
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.0.0" PrivateAssets="All" />
Prepare the app for a framework-dependent deployment by executing a command such as the following at the project root:
> dotnet publish -c Release
A .PrecompiledViews.dll file, containing the compiled Razor views, is produced when precompilation succeeds.
关于razor - 为什么 dotnet 命令行构建在 MVC View 错误时失败,而 VS2017 没有?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48684036/