0

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?

1 Answer 1

3

.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();
  }
1
  • For people like me who wants to find the docs about CopyRazorGenerateFilesToPublishDirectory: see official docs : "When true, copies RazorGenerate items (.cshtml) files to the publish directory. Typically, Razor files aren't required for a published app if they participate in compilation at build-time or publish-time. Defaults to false."
    – itminus
    Jun 19, 2020 at 2:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.