shared-project

Visual Studio Successfully Using Shared Project wwwroot Files on Publish but not on Build/Run


The Background

I'm working in Visual Studio 2019. I have a C# Web APP Razor Pages project which references several C# Class Projects and one C# Shared Project for files common across several other related Web App projects.

The Problem

When I publish the web app project to my local or a remote IIS instance, no problem, the shared project files, all of them, are present and accounted for and work as expected.

However, when I "just" build/run then view it in the browser, the shared project Helper and Pages are as expected, but the shared project wwwroot files are all missing in action.

The Want

I could, but do not wish to have to, publish locally every single time I need to see / troubleshoot anything that's in the shared project, so ...

I would like to find a way for the shared project wwwroot folder assets to be useable on Build/Run -AND- to be able to do it in such as way as to not to adversely impact the Publish process in which it is currently working as expected.

The Deets

The example folder structure for the relevant projects (probably tmi but detailed enough that if I have to do soemthing with paths from one project to the other, these relative paths would be correct enough for an example to be made) ...




Where to go from here

As I was laying out what I'd tried and the questions I had, some things fell into place. Love the process, hate the pre-process hair-pulling. So I've answered my own question here, but will go ahead and post this so I can refer back to it later and so maybe it will help someone else keep a few hairs. Also, if there are other ways to do this, bring 'em on -- always good to consider alternatives. :0)


Solution

  • After research and putting bits and pieces together...

    ... the following is working for me on Build/Run and on Publish (be it via right-click-project-select-publish or via command line msbuild w/ deploy on build).


    (1) store files destined for wwwroot in another folder, say, _wwwroot, and adjust the project files so they don't get published directly:

    web app project's .csproj:

     <ItemGroup>
        <Content Remove="_wwwroot\**\*" />
      </ItemGroup>
    

    shared project's .projitems (note the absence here of CopyToPublishDirectory or CopyToOutputDirectory tags):

      <ItemGroup>
        <Folder Include="$(MSBuildThisFileDirectory)_wwwroot\**\*" />
      </ItemGroup>
    

    (2) add build events to the web app project

    a pre-build event to remove everything in the web app project's wwwroot folder (so each build starts with a clean slate)

      rd /s /q "$(ProjectDir)wwwroot"
    

    and 2 post-build events in each web app project

     xcopy /E /Y /I "$(ProjectDir)_wwwroot" "$(ProjectDir)wwwroot"
     xcopy /E /Y /I "$(ProjectDir)..\..\..\MyName.Common.Presentation.Web\_wwwroot" "$(ProjectDir)wwwroot"
    

    (3) set each web app project's wwwroot to be ignored on check-in to source control since the _wwwroot folders will be checked-in and will be considered the source of record

    (4) reference everything that will be in the wwwroot folder normally, just as if they are always there since they will be present in wwwroot when needed