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>
Dean North在他的原始文章中直接提到了使用预编译视图的程序集。
<ItemGroup>
<Reference Include="DashboardExample.PrecompiledViews">
<HintPath>..\DashboardExample\bin\Debug\netcoreapp1.1\DashboardExample.PrecompiledViews.dll</HintPath>
</Reference>
</ItemGroup>
Debug
)构建的程序集。在我上面的项目文件中,我使用了单独的目标来在构建和发布过程中复制ChildApplication.PrecompiledViews.dll
。MvcApplication
目录下运行了dotnet publish -c Release
命令?对我来说,它完美地运行了。这是一个发布执行的截图,请与你的运行输出进行比较。 - CodeFuller.csproj
中即可,无需复制文件或任何混乱的操作。翻译后:我一般会把它们添加到.csproj
文件中,不需要复制文件或其他麻烦的操作。 <PropertyGroup>
<TargetFramework>netcoreapp2.2</...
...
<MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>
<ItemGroup>
...
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.ViewCompilation" Version="2.2.0" />
</ItemGroup>
TargetFramework
是什么?并且你尝试过将MvcRazorCompileOnPublish
隐式设置为 true 吗? - Set