MSBuild does not copy views to the output folder

Deadly 提交于 2020-06-28 10:22:06

问题


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:


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


来源:https://stackoverflow.com/questions/61869295/msbuild-does-not-copy-views-to-the-output-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!