nima ansari
nima ansari

Reputation: 582

MSBuild does not copy views to the output folder

After upgrading to .NET Core 3.1, my home-made build system broke. Here's the problem. I use MSBuild to publish a project in a CI/CD pipeline. This is my code:

"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe" C:\Company\Solution\Solution.sln /t:UserPanel /p:DeployOnBuild=true /p:PublishProfile=DeployUserPanel /p:SolutionDir=C:\Company\Solution /property:PublishFolder=C:\Publish\Solution\UserPanel /t:Publish

The exact same code would publish .cshtml files to the output directory in .NET Core 2.2. But now I need to manually copy/paste them from my UserPanel project into the publish folder, which breaks the CI/CD automation of course.

What should I do to fix it?

Upvotes: 1

Views: 888

Answers (1)

Martin Ullrich
Martin Ullrich

Reputation: 100701

.NET Core moved to pre-compiling the cshtml files so they are no longer needed in production. You will see that there should also be a [YourApp].Views.dll in the publish output which contains the compiled views.

If you absolutely need the old behavior, refer to Razor file compilation in ASP.NET Core for documentation on configuring Razor runtime compilation.

It comes down to changing the csproj to

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <CopyRefAssembliesToPublishDirectory>true</CopyRefAssembliesToPublishDirectory>
    <CopyRazorGenerateFilesToPublishDirectory>true</CopyRazorGenerateFilesToPublishDirectory>
  </PropertyGroup>

And changing your Startup.cs to include .AddRazorRuntimeCompilation() on the call to configure MVC:

  public void ConfigureServices(IServiceCollection services)
  {
      services.AddControllersWithViews()
          .AddRazorRuntimeCompilation();
  }

Upvotes: 4

Related Questions