我打赌你使用Self-contained deployment,即使用类似命令发布
dotnet publish --configuration Release --runtime win-x64
这会生成包含所有依赖项的可执行文件,包括 .NET Core 二进制文件。
Razor view compilation and precompilation 文章包含以下警告:
Razor 视图预编译当前在执行
ASP.NET Core 2.0 中的自包含部署 (SCD)。该功能将
2.1 版本时可用于 SCD。
所以如果你想使用预编译的 Razor 视图,你应该使用Framework-dependent deployment,即使用以下命令发布:
dotnet publish --configuration 发布
在这种情况下,Razor 视图是预编译的(默认情况下),您会在其他应用程序二进制文件中找到 YourAppName.PrecompiledViews.dll。
更新(用于库项目中的预编译视图)
我的原始答案与通常的 ASP.NET Core MVC 应用程序有关,但问题是特定于包含预编译视图(即自包含 UI)的项目库。
默认情况下,ASP.NET Core 在发布期间预编译视图,但是对于存储在库项目中的视图,情况并非如此。有一个github issue 专门解决这个问题。该讨论很长,但是ends up 得出的结论是,目前我们仍然需要使用带有自定义目标的解决方案来进行 Razor Views 预编译。它与问题引用的article中描述的方法基本相同。
我已经使用ChildApplication 和主要MvcApplication 设置了测试解决方案,并使预编译视图同时适用于构建和发布。
这里是 ChildApplication 的 csproj(跳过默认 ASP.NET Core MVC 项目的部分):
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
</PropertyGroup>
<!-- ... -->
<Target Name="SetMvcRazorOutputPath">
<PropertyGroup>
<MvcRazorOutputPath>$(OutputPath)</MvcRazorOutputPath>
</PropertyGroup>
</Target>
<Target Name="_MvcRazorPrecompileOnBuild" DependsOnTargets="SetMvcRazorOutputPath;MvcRazorPrecompile" AfterTargets="Build" Condition=" '$(IsCrossTargetingBuild)' != 'true' " />
<Target Name="IncludePrecompiledViewsInPublishOutput" DependsOnTargets="_MvcRazorPrecompileOnBuild" BeforeTargets="PrepareForPublish" Condition=" '$(IsCrossTargetingBuild)' != 'true' ">
<ItemGroup>
<_PrecompiledViewsOutput Include="$(MvcRazorOutputPath)$(MSBuildProjectName).PrecompiledViews.dll" />
<_PrecompiledViewsOutput Include="$(MvcRazorOutputPath)$(MSBuildProjectName).PrecompiledViews.pdb" />
<ContentWithTargetPath Include="@(_PrecompiledViewsOutput->'%(FullPath)')" RelativePath="%(_PrecompiledViewsOutput.Identity)" TargetPath="%(_PrecompiledViewsOutput.Filename)%(_PrecompiledViewsOutput.Extension)" CopyToPublishDirectory="PreserveNewest" />
</ItemGroup>
</Target>
这里是父 MvcApplication 的 csproj:
<!-- ... -->
<ItemGroup>
<ProjectReference Include="..\ChildApplication\ChildApplication.csproj" />
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy "$(ProjectDir)\..\ChildApplication\bin\$(ConfigurationName)\netcoreapp2.0\ChildApplication.PrecompiledViews.dll" "$(TargetDir)" /Y /I" />
</Target>
<Target Name="AddPayloadsFolder" AfterTargets="Publish">
<Exec Command="xcopy "$(ProjectDir)\..\ChildApplication\bin\$(ConfigurationName)\netcoreapp2.0\ChildApplication.PrecompiledViews.dll" "$(PublishDir)" /Y /I" />
</Target>
his original article 中的 Dean North 添加了对带有预编译视图的程序集的直接引用。
<ItemGroup>
<Reference Include="DashboardExample.PrecompiledViews">
<HintPath>..\DashboardExample\bin\Debug\netcoreapp1.1\DashboardExample.PrecompiledViews.dll</HintPath>
</Reference>
</ItemGroup>
这种方法并不完美,因为它使用由特定配置构建的程序集(此处为Debug)。在上面的项目文件中,我使用了在构建和发布期间复制 ChildApplication.PrecompiledViews.dll 的单独目标。
这里是 Sample Solution on GitHub,包含父项目和子项目。