Publish the Views with asp.net core

edit your.csproj file and add PreserveCompilationContext as true and MvcRazorCompileOnPublish as false

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<PreserveCompilationContext>true</PreserveCompilationContext>
<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
</PropertyGroup>

then views will be included in publish

Edit: As of version 2.1 it is not possible to use Razor Class Libraries, and instead of embedding views they can be pre-compiled. Local views in the web app can still override views in the class library. In the new scenario you would remove the PreserveCompilationContext and MvcRazorCompileOnPublish settings and just use the default values. This way all views in the application will be pre-compiled and no .cshtml files will be included in the publish output.


Joe's answer is for .Net Core 2.

In .Net Core 3, if you are using the default services.AddControllersWithViews() in your Startup.cs then you need to use RazorCompileOnPublish.

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <PreserveCompilationContext>true</PreserveCompilationContext>
    <RazorCompileOnPublish>false</RazorCompileOnPublish>
  </PropertyGroup>

Also, if you need to enable Razor Runtime Compilation in Core 3, you require to install the "Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" package, then add the AddRazorRuntimeCompilation.

services.AddControllersWithViews()
        .AddRazorRuntimeCompilation();

Tags:

Asp.Net Core