razor - 为什么 dotnet 命令行构建在 MVC View 错误时失败,而 VS2017 没有?

标签 razor msbuild visual-studio-2017 .net-core asp.net-core-mvc

我正在 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/

相关文章:

c# - 使用 HTML Helpers 时列表模型绑定(bind)索引不正确

c# - C#编译错误: CS0122 & CS1061

jenkins - msbuild 与命令行与 Jenkins 的行为不同

node.js - 我可以关闭与 Visual Studio 2017 关联的 Node.js 服务器进程吗?

c# - Visual Studio 2017 中缺少创建单元测试上下文菜单选项?

asp.net-mvc - VS 2015 中禁用的标签助手

asp.net-mvc - 有没有一种很好的方法来控制显示模板应该如何呈现空字段?

msbuild - 使用 MSBuild 通过 VS2017 构建 SSDT 项目失败

jquery - 如何动态创建/删除元素并允许模型绑定(bind)启动?

msbuild - .NET Core(非 ASP.NET Core)项目的文件可以嵌套在 VS2017 解决方案资源管理器中吗?