在 net core 2.0 中发布项目时未复制视图 visual studio 2017
Views not being copied when publish the project in netcore 2.0 visual studio 2017
我在 VS 2017 中创建了一个 ASP.NET Core 2.0 项目。当我发布我的项目时,Views
文件夹不存在,但 wwwroot
文件夹存在。
我可以在我的 .csproj 文件中使用以下内容进行设置:
<ItemGroup>
<Content Update="appsettings.json;web.config" CopyToOutputDirectory="PreserveNewest"/>
<Content Update="Views\**\*" CopyToOutputDirectory="PreserveNewest" />
<Content Update="wwwroot\**\*" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
但没用。
ASP.NET Core MVC 有一个 precompilation 特性,可以通过引用 Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
包来添加。 MvcRazorCompileOnPublish
属性 配置的功能 enabling/disabling 是 .csproj。
并且默认情况下,包添加在 ASP.NET Core 2.0 MVC 应用程序中:
If you're targeting ASP.NET Core 2.0 or higher on netcoreapp2.0, a reference to the Microsoft.AspNetCore.Mvc.Razor.ViewCompilation package is added by Microsoft.AspNetCore.All and you do not need to explicitly reference it.
The ASP.NET Core 2.x project templates implicitly set MvcRazorCompileOnPublish
to true by default, which means this node can be safely removed from the .csproj file:
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish>
</PropertyGroup>
如果你去发布输出文件夹,你会看到类似 <project_name>.PrecompiledViews.dll
的 dll,其中包含你的视图。
它可能会默认生成 <project_name>.Views.dll
。您需要在项目 .csproj
文件中设置 MvcRazorCompileOnPublish
属性 false
。默认为 true
。
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
</PropertyGroup>
在 3.1 上,我已经尝试了以上所有方法,但没有出现 .cshtml 文件。有趣的是,我确实将 .cs 文件复制到发布文件夹。即在我的 csproj 中,前两个有效 - 另一个无效
<ItemGroup>
<Compile Update="Pages\AisLayout.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="Pages\Application.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
</ItemGroup>
<ItemGroup>
<Content Update="Pages\ApplicationState.cshtml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="Pages\Cookies.cshtml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
如果我手动复制文件,我可以通过动态重新编译来修改它们(如预期的那样)。注意:.cshtml 文件没有代码隐藏(长话短说)。
更多建议?
在.NET Core 3.1或更高版本中,我们需要在.csproj中添加如下代码:
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<CopyRazorGenerateFilesToPublishDirectory>true</CopyRazorGenerateFilesToPublishDirectory>
</PropertyGroup>
根据 docs:
When true
, copies RazorGenerate
items (.cshtml) files to the publish directory. Typically, Razor files aren't required for a published app if they participate in compilation at build-time or publish-time. Defaults to false
.
我在 VS 2017 中创建了一个 ASP.NET Core 2.0 项目。当我发布我的项目时,Views
文件夹不存在,但 wwwroot
文件夹存在。
我可以在我的 .csproj 文件中使用以下内容进行设置:
<ItemGroup>
<Content Update="appsettings.json;web.config" CopyToOutputDirectory="PreserveNewest"/>
<Content Update="Views\**\*" CopyToOutputDirectory="PreserveNewest" />
<Content Update="wwwroot\**\*" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
但没用。
ASP.NET Core MVC 有一个 precompilation 特性,可以通过引用 Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
包来添加。 MvcRazorCompileOnPublish
属性 配置的功能 enabling/disabling 是 .csproj。
并且默认情况下,包添加在 ASP.NET Core 2.0 MVC 应用程序中:
If you're targeting ASP.NET Core 2.0 or higher on netcoreapp2.0, a reference to the Microsoft.AspNetCore.Mvc.Razor.ViewCompilation package is added by Microsoft.AspNetCore.All and you do not need to explicitly reference it.
The ASP.NET Core 2.x project templates implicitly set
MvcRazorCompileOnPublish
to true by default, which means this node can be safely removed from the .csproj file:<PropertyGroup> <TargetFramework>netcoreapp2.0</TargetFramework> <MvcRazorCompileOnPublish>true</MvcRazorCompileOnPublish> </PropertyGroup>
如果你去发布输出文件夹,你会看到类似 <project_name>.PrecompiledViews.dll
的 dll,其中包含你的视图。
它可能会默认生成 <project_name>.Views.dll
。您需要在项目 .csproj
文件中设置 MvcRazorCompileOnPublish
属性 false
。默认为 true
。
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
</PropertyGroup>
在 3.1 上,我已经尝试了以上所有方法,但没有出现 .cshtml 文件。有趣的是,我确实将 .cs 文件复制到发布文件夹。即在我的 csproj 中,前两个有效 - 另一个无效
<ItemGroup>
<Compile Update="Pages\AisLayout.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="Pages\Application.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
</ItemGroup>
<ItemGroup>
<Content Update="Pages\ApplicationState.cshtml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="Pages\Cookies.cshtml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
如果我手动复制文件,我可以通过动态重新编译来修改它们(如预期的那样)。注意:.cshtml 文件没有代码隐藏(长话短说)。
更多建议?
在.NET Core 3.1或更高版本中,我们需要在.csproj中添加如下代码:
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<CopyRazorGenerateFilesToPublishDirectory>true</CopyRazorGenerateFilesToPublishDirectory>
</PropertyGroup>
根据 docs:
When
true
, copiesRazorGenerate
items (.cshtml) files to the publish directory. Typically, Razor files aren't required for a published app if they participate in compilation at build-time or publish-time. Defaults tofalse
.