使用 ms build 预编译 asp.net 视图
您应该使用参数/p:PrecompileBeforePublish=true 而不是/p:MvcBuildViews=true。
MvcBuildViews 通常被错误地认为是激活后会导致预编译视图的东西。实际上。将视图包含在构建过程中只是一件事,但它不会将它们编译到项目二进制文件夹中。
当我们选中Precompile during publish复选框并取消选中文件发布选项上的复选框Allow precompiled site to be updateable时,我们将在FolderProfile.pubxml文件中获得以下属性设置:
<PropertyGroup>
<PrecompileBeforePublish>True</PrecompileBeforePublish>
<EnableUpdateable>False</EnableUpdateable>
</PropertyGroup>
所以如果你想对 msbuild 工具做同样的事情,我们应该使用参数:
/p:PrecompileBeforePublish=true;EnableUpdateable=false
此外,因为这些参数存储在 .pubxml 文件中(在解决方案资源管理器的属性节点中的 PublishProfiles 下)。它们现在设计为可以签入并与团队成员共享。这些文件现在是 MSBuild 文件,您可以根据需要自定义它们。为了从命令行发布,只需传递 DeployOnBuild=true 并将 PublishProfile 设置为配置文件的名称:
msbuild.exe "TestPrecompiled.csproj" /p:DeployOnBuild=true /p:PublishProfile=FolderProfile.pubxml
当然,你可以同时使用参数和.pubxml文件,命令行中的参数会覆盖.pubxml文件中的属性:
msbuild.exe "TestPrecompiled.csproj" /p:DeployOnBuild=true /p:PublishProfile=FolderProfile.pubxml /p:PrecompileBeforePublish=true;EnableUpdateable=false
发布完成后,打开发布文件夹中的.cshtml文件,我们会得到This is a marker file generated by the precompilation tool, and should not be deleted!这一行,就像他们从VS发布时一样:
有关更多详细信息,请参阅Precompiling ASP.NET WebForms and MVC Views with MSBuild。