Search code examples
asp.net-coreasp.net-core-mvcvisual-studio-2017asp.net-core-2.0

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.


Solution

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