Reputation: 473
I have createa a ASP.NET Core 2.0 project in VS 2017. When I publish my project the Views
folder are not there but the wwwroot
folder is.
This I can setup in my .csproj file with the following:
<ItemGroup>
<Content Update="appsettings.json;web.config" CopyToOutputDirectory="PreserveNewest"/>
<Content Update="Views\**\*" CopyToOutputDirectory="PreserveNewest" />
<Content Update="wwwroot\**\*" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
but didn't work.
Upvotes: 16
Views: 12209
Reputation: 895
For anyone ended up here struggling with dotnet 6 or higher:
The Razor compiler no longer produces a separate Views.dll file that contains the CSHTML views defined in an application.
Upvotes: 1
Reputation: 473
In .NET Core 3.1 or later, we need to add the following code in .csproj:
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<CopyRazorGenerateFilesToPublishDirectory>true</CopyRazorGenerateFilesToPublishDirectory>
</PropertyGroup>
According to the 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
.
Upvotes: 19
Reputation: 49789
ASP.NET Core MVC has a precompilation feature, that could be added by referencing the Microsoft.AspNetCore.Mvc.Razor.ViewCompilation
package. Feature enabling/disabling configured by MvcRazorCompileOnPublish
property is .csproj.
And by default, the package is added in ASP.NET Core 2.0 MVC applications:
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.
and precompilation is enabled:
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>
If you go to publish output folder, you will see dll like <project_name>.PrecompiledViews.dll
which contains your views.
Upvotes: 18
Reputation: 125
On 3.1, I have tried all of the above with no .cshtml files appearing. Interesting, I do get .cs files copied across to publish folders. i.e. in my csproj, the first two work - the other does not
<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>
If I manually copy the files, I am able to modify them with dynamic recompile occurring (as expected). Note: The .cshtml has no code behind files (long story).
More suggestions?
Upvotes: 2
Reputation: 6514
It might be generating <project_name>.Views.dll
by default. You need to set MvcRazorCompileOnPublish
property false
in your project .csproj
file. By default it is true
.
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
</PropertyGroup>
Upvotes: 4