Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Views not being copied when publish the project in netcore 2.0 visual studio 2017

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.

like image 635
Anil Avatar asked Oct 11 '17 09:10

Anil


People also ask

How to use publish profile Visual Studio?

On the computer where you have the ASP.NET project open in Visual Studio, right-click the project in Solution Explorer, and choose Publish. If you have previously configured any publishing profiles, the Publish pane appears. Click New or Create new profile. Select the option to import a profile.

Where is. Pubxml file?

pubxml and are located in the PublishProfiles folder. The PublishProfiles folder is under Properties in a C# web application project, under My Project in a VB web application project, or under App_Data in a web site project. Each . pubxml file contains settings that apply to one publish profile.

What command does Visual Studio use to publish?

Basic command-line publishing 2\publish\. The dotnet publish command calls MSBuild, which invokes the Publish target. Any parameters passed to dotnet publish are passed to MSBuild.


2 Answers

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.

like image 72
Set Avatar answered Oct 21 '22 23:10

Set


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, 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.

like image 36
Anil Avatar answered Oct 21 '22 22:10

Anil