EricImhauser
EricImhauser

Reputation: 791

Publish the Views with asp.net core

In an asp.net core 2.0 project, I would like to force the publish of Views because I need them at runtime. Any clues?

Upvotes: 14

Views: 17389

Answers (4)

Klesun
Klesun

Reputation: 13723

None of other solutions worked for me™, but this one did: https://github.com/dotnet/aspnetcore/issues/36583#issuecomment-930319346

  1. Include package (to enable runtime compilation for Razor pages &) views) Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
  2. In ConfigureServices() (to enable runtime compilation for Razor pages &) views)
    services.AddMvc(
    //....
    //...
    ).AddRazorRuntimeCompilation();
    
  3. In web.csproj add the following:
    <PropertyGroup>
      <!-- To publish views instead of views.dll-->
      <EnableDefaultRazorGenerateItems>false</EnableDefaultRazorGenerateItems>
      <EnableDefaultRazorComponentItems>false</EnableDefaultRazorComponentItems>
      <CopyRazorGenerateFilesToPublishDirectory>true</CopyRazorGenerateFilesToPublishDirectory>
    </PropertyGroup>
    
    <ItemGroup>
       <!-- Explicitly include each .cshtml files in the subfolders under Views  -->
      <Content Update="Views\**\*.cshtml" CopyToPublishDirectory="PreserveNewest" /> 
    </ItemGroup>
    

Upvotes: 1

Satish
Satish

Reputation: 1

And also you need to install '.Net Core hosting bundle for IIS' before publishing your site.

Upvotes: 0

Mohammad Karimi
Mohammad Karimi

Reputation: 4223

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();

Upvotes: 11

Joe Audette
Joe Audette

Reputation: 36736

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.

Upvotes: 39

Related Questions