Taking inspiration from here: http://rion.io/2017/03/22/sharing-is-caring-using-shared-projects-in-asp-net/, I'm trying to use Shared Projects to share static assets (e.g. css / js) between multiple ASP.Net Core projects.
I created a shared project and added this file in this folder hierarchy:
I then added a reference to this shared project from my ASP.Net Core web project. However, I'm not able to access this file when I run the project. I tried these URLs (among many others):
http://localhost:50000/assets.style.css
http://localhost:50000/wwwroot/assets/style.css
They all result in 404. It seems like I either haven't found a way to make these files available in my web project or I'm referencing them incorrectly. Anyone know how to make this work?
Here's how I solved this problem. I didn't use a Shared Project because I couldn't get that to work. Instead, I used
PhysicalFileProvider
and it works like charm.To do this, I created a new folder in
src
. I called itassets
so my Solution Explorer looks something like this:I put all of the shared static assets (e.g. css, JavaScript, images) in
/src/assets
. Now I map/src/assets
to thewwwroot/assets
folders inFoo.Web1
andFoo.Web2
projects usingPhysicalFileProvider
. InStartup.cs
'sConfigure
method, I add this:Now any request that comes to
/assets
will be served from the/src/assets
folder that can be shared byFoo.Web1
andFoo.Web2
. All changes in/src/assets
will be reflected in real-time. This work great in dev, but when it comes to production, I don't do this. Instead, I copy the files from/src/assets
toFoo.Web1/wwwroot/assets
andFoo.Web2/wwwroot/assets
in my deployment script so this remapping doesn't need to happen.