question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

dotnet publish not copying extra View files on Linux

See original GitHub issue

I am working on an ASP.NET Core 2.0 project, and I have some .cshtml files in my Views directory that are used as e-mail templates and rendered using a wrapper service around the Razor engine. My project is configured to precompile views during publish to speed up rendering later down the line, which happens successfully, except that the Razor engine can’t find the Email views in Production because they apparently aren’t included in the view precompilation (as they’re never referenced by a Controller).

I have these additional .cshtml files specified in my .csproj file in an <ItemGroup> as

<None Include="Views\Email\**\*">
    <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    <CopyToPublishDirectory>Always</CopyToPublishDirectory>
</None>

When I run dotnet publish on Windows, the Views are correctly copied to the bin\Debug\netcoreapp2.0\publish\ directory as expected. However, when I run the same command on Linux (in the same repo), they aren’t copied to the specified publish directory, so I get a 500 error when the site tries to send e-mails.

The output of dotnet --info on Windows, where the views are copied correctly, is the following:

.NET Command Line Tools (2.1.0-preview1-007042)

Product Information:
 Version:            2.1.0-preview1-007042
 Commit SHA-1 hash:  e94a82c9a3

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.14393
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\2.1.0-preview1-007042\

Microsoft .NET Core Shared Framework Host

  Version  : 2.0.0
  Build    : e8b8861ac7faf042c87a5c2f9f2d04c98b69f28d

The output on Linux is the following:

.NET Command Line Tools (2.0.0)

Product Information:
 Version:            2.0.0
 Commit SHA-1 hash:  cdcd1928c9

Runtime Environment:
 OS Name:     centos
 OS Version:  7
 OS Platform: Linux
 RID:         centos.7-x64
 Base Path:   /usr/share/dotnet/sdk/2.0.0/

Microsoft .NET Core Shared Framework Host

  Version  : 2.0.0
  Build    : e8b8861ac7faf042c87a5c2f9f2d04c98b69f28d

From what I can tell, 2.1.0-preview is not available for CentOS yet. Is the version difference why it succeeds on Windows and not Linux, or is there something else I’m missing?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

5reactions
ArcanoxDragoncommented, Sep 6, 2017

Well, I ended up solving my own problem now after an hour of sifting through StackOverflow and GitHub issues, so I’ll describe my fix for anyone else in a similar scenario.

I added a project property in one of the <PropertyGroup> sections for my e-mail template files, and appended that onto the <DefaultItemExcludes> property:

<PropertyGroup>
    <EmailTemplatesPattern>Views\Email\**\*</EmailTemplatesPattern>
    <DefaultItemExcludes>$(DefaultItemExcludes);$(EmailTemplatesPattern)</DefaultItemExcludes>
</PropertyGroup>

This apparently causes the Razor view precompilation to skip those View files, so the following <ItemGroup> entries correctly copy the files on output:

<ItemGroup>
    <None Include="$(EmailTemplatesPattern)">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        <CopyToPublishDirectory>Always</CopyToPublishDirectory>
    </None>
</ItemGroup>
3reactions
ppumkincommented, Jan 25, 2018

I had the same issue on Windows Core 2.x - I had to set

  • Build Action None (or anything other than content - go figure)
  • Copy To Output - Copy Always (obvious, and per file)

But then I got Cannot find compilation library location for package Microsoft.Win32.Registry error which is described as per https://github.com/dotnet/core-setup/issues/2113 and fixable by manually adding <MvcRazorExcludeRefAssembliesFromPublish>false</MvcRazorExcludeRefAssembliesFromPublish> which includes a load of extra DLL’s into a ref folder during publish…

Works now! Phew… (On Windows)

Read more comments on GitHub >

github_iconTop Results From Across the Web

BundlerMinifierCore files are not copied on dotnet publish
Since wwwroot is already included here and the copying of other files like Views works, I thought there is an issue about execution...
Read more >
dotnet publish command - .NET CLI
dotnet publish - Publishes the application and its dependencies to a folder for deployment to a hosting system. Synopsis .NET CLI Copy.
Read more >
NET application publishing overview
Learn about the ways to publish a .NET application. .NET can publish platform-specific or cross-platform apps. You can publish an app as ...
Read more >
Visual Studio publish profiles (.pubxml) for ASP.NET Core ...
Learn how to create publish profiles in Visual Studio and use them for managing ASP.NET Core app deployments to various targets.
Read more >
Create a single file for application deployment - .NET
Publish a single file application using the dotnet publish command. Add <PublishSingleFile>true</PublishSingleFile> to your project file. This ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found