Static Files (JS, CSS, etc) in Visual Studio C# Shared Project

I've seen other websites state that it's possible to use a shared project for Javascript, CSS, and other static files. I already have some C# files in my shared project.

I'm able to create the shared project and add the files to it. I can even find these files that are in my shared project in my web application using Intellisense. However, I always get a 404 error whenever I am running my web app locally. It cannot find the files that are in the shared project.

What can I do to fix this?

Thanks!


When you add a file to your project in Visual Studio it defaults to not copy the file to your output directory.

To automatically copy the files to your output directory when you build your project: select the files in Solution Explorer, set the "Build Action" to Content and "Copy to Output Directory" to Copy always or Copy if newer.


I came with a solution to this, the only thing you need to do is to add following code to the file YourSharedProjectName.projitems (which should be in your shared project folder):

<ItemGroup>
  <None
      Include="$(MSBuildThisFileDirectory)Content\**\*.*"
      Link="..\Content\%(RecursiveDir)%(Filename)%(Extension)"
      CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

In this case I wanted to have Content folder shared among different web projects in the solution, so I created "Content" directory in SharedProject and added there directories and files I need (Scripts, Styles, etc.). Then after adding this MSBuild instruction in .projitems file, all the files get copied to the corresponding "Content" directory of each project. This way you can even have some project specific files in its own "Content" as long as there are no identical file names.