如何在ASP.NET Core 2.0中预编译视图? [英] How to precompile views in ASP.NET Core 2.0?

查看:82
本文介绍了如何在ASP.NET Core 2.0中预编译视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据此文章.我遗漏了一些东西,因为根据> ,ASP.NET Core 2.0默认情况下会预编译视图.最后,我将其发布到一个成功结束的文件夹中,但是缺少我的precompiledviews.dll.我尝试在.csproj中显式设置它,但是没有运气.

I set up my solution according to this article. I left out some of the things because according to this, ASP.NET Core 2.0 precompiles views by default. In the end, I publish it to a folder, which ends successfully, but my precompiledviews.dll is missing. I tried setting it explicitly in .csproj, but no luck.

解决方案中的两个项目都是默认的MVC模板.

Both of the projects inside the solution are just default MVC templates.

推荐答案

我敢打赌,您使用

I bet you use Self-contained deployment, i.e. publish with command like

dotnet发布-配置发布--runtime win-x64

dotnet publish --configuration Release --runtime win-x64

生成具有所有依赖项(包括.NET Core二进制文件)的可执行文件.

that results to executable file with all dependencies including .NET Core binaries.

剃刀视图的编译和预编译文章包含以下警告:

执行以下操作时,剃刀视图预编译当前不可用 ASP.NET Core 2.0中的自包含部署(SCD).该功能将 在2.1发行时可用于SCD.

Razor view precompilation is currently unavailable when performing a self-contained deployment (SCD) in ASP.NET Core 2.0. The feature will be available for SCDs when 2.1 releases.

因此,如果要使用预编译的Razor视图,则应使用依赖于框架的部署,即使用以下命令进行发布:

So if you want to use precompiled Razor views, you should use Framework-dependent deployment, i.e. publish with the following command:

dotnet发布-配置发布

dotnet publish --configuration Release

在这种情况下,Razor视图是预编译的(默认情况下),您会在其他应用程序二进制文件中找到YourAppName.PrecompiledViews.dll.

In this case Razor views are precompiled (by default) and you will find YourAppName.PrecompiledViews.dll among other application binaries.

更新(针对库中的预编译视图"项目)

UPDATE (for Precompiled Views in Library project)

我最初的答案与通常的ASP.NET Core MVC应用程序有关,但是问题是特定于项目库的,这些项目库包含预编译的视图(也称为自包含" UI).

My original answer relates to usual ASP.NET Core MVC application, however the question is specific to project library holding precompiled views aka Self-Contained UI.

ASP.NET Core在发布期间默认情况下会预编译视图,但是对于存储在库项目中的视图则不是这种情况.有一个 github问题专门解决此问题.该讨论已经持续了很长时间,但是它最终得出的结论是目前,我们仍然需要使用具有自定义目标的解决方案来进行Razor Views预编译.它与 article 被问题引用.

ASP.NET Core precompiles views by default during the publish, however this is not the case for the views stored in library project. There is a github issue devoted to this problem. That discussion is pretty much long, however it ends up with the conclusion that for this moment we still need to use solution with custom targets for Razor Views precompilation. It basically the same approach as described in the article referenced by the question.

我已经使用ChildApplication和主MvcApplication设置了测试解决方案,并使预编译的视图可同时用于构建和发布.

I've setup test solution with ChildApplication and main MvcApplication and made precompiled views working both for the build and publish.

以下是ChildApplication的csproj(跳过默认ASP.NET Core MVC项目的部分):

Here is csproj for ChildApplication (skipping sections of default ASP.NET Core MVC project):

<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:

Here is csproj for parent MvcApplication:

<!-- ... -->

<ItemGroup>
    <ProjectReference Include="..\ChildApplication\ChildApplication.csproj" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="xcopy &quot;$(ProjectDir)\..\ChildApplication\bin\$(ConfigurationName)\netcoreapp2.0\ChildApplication.PrecompiledViews.dll&quot; &quot;$(TargetDir)&quot; /Y /I" />
</Target>

<Target Name="AddPayloadsFolder" AfterTargets="Publish">
    <Exec Command="xcopy &quot;$(ProjectDir)\..\ChildApplication\bin\$(ConfigurationName)\netcoreapp2.0\ChildApplication.PrecompiledViews.dll&quot; &quot;$(PublishDir)&quot; /Y /I" />
</Target>

Dean North在他的原始文章中为具有预编译视图的程序集添加直接引用.

Dean North in his original article adds direct reference to assembly with precompiled views.

<ItemGroup>
    <Reference Include="DashboardExample.PrecompiledViews">
        <HintPath>..\DashboardExample\bin\Debug\netcoreapp1.1\DashboardExample.PrecompiledViews.dll</HintPath>
    </Reference>
</ItemGroup>

这种方法不是完美的,因为它使用以特定配置(此处为Debug)构建的程序集.在上面的项目文件中,我使用了分别在构建和发布期间复制ChildApplication.PrecompiledViews.dll的目标.

Such approach isn't perfect because it uses assembly built with specific configuration (Debug here). In my project file above, I use separate targets that copy ChildApplication.PrecompiledViews.dll during the build and publish.

这里是 GitHub上的示例解决方案 包含父项目和子项目.

Here is Sample Solution on GitHub with both parent and child projects.

这篇关于如何在ASP.NET Core 2.0中预编译视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆