13

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?

EricImhauser
  • 701
  • 2
  • 7
  • 17
  • Possible duplicate of [Asp.net core 2 - Files are not published](https://stackoverflow.com/questions/49444457/asp-net-core-2-files-are-not-published) – Dominik May 27 '19 at 20:14

3 Answers3

38

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 Audette
  • 35,330
  • 11
  • 106
  • 99
  • Thanks, it works!! I would have been nice to leave the compile on publish and still publish (some of ) the views. FYI, I need some views because I use them as email templates. – EricImhauser Sep 17 '17 at 17:30
  • I also use the Razor engine for my email templates and it requires it to be found using IRazorViewEngine.FindView which requires a file path to locate the view. – ravetroll Apr 13 '18 at 12:01
  • But these view file is not working at website online. when i am uploading these files using filezilla than it is not working – Love Pandey Aug 03 '19 at 20:00
9

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();
Mohammad Karimi
  • 4,151
  • 2
  • 21
  • 19
  • This was helpful to an extent. I'm using the RazorEngine (.net core) package to generate emails in our service, but when attempting to compile the template from the published files, an error occurs. I narrowed it down to a missing ProjectName.Views.dll file. If I remove the .csproject changes, a DLL is published, but not the Views folder. Is there a way to reference the embedded resource of the cshtml file from my code instead of trying to read a file? – Mathachew Nov 14 '20 at 00:02
  • This worked for me. Suddenly in our .net 6.0 project that was publishing fine, stopped working on IIS. Could not fund any views at all. Worked fine when debugging locally in Rider. Spent 8h trying different things, eventually came across this post and the combination of AddRazorRuntimeCompilation() with the changes in .csproj worked. Thank you. – Varin Nov 02 '22 at 11:22
0

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

Satish
  • 1