Sharing resources between projects in a Visual Studio solution

We have a visual studio solution in which two web application projects share certain view, css, font, javascript and image files. Copying each of these resources twice to each project introduces the risk that when one is changed in the future, the developer forgets about the other and the two projects become out of sync. In order to mitigate this risk I wanted to be able to reference the same resource from both projects.

Firstly I created a new folder in my solution where all the shared resources would be stored, and created subfolders under this to order the resources, one for fonts, one for images etc.


Then I created physical folders which matched these solution folders.

Createphysicalfolder 500X224

Then I copied the physical resources into these folders. These folders were committed to git. This is where the resources would be kept. Then to add the resources into my solution I right clicked on the folder in the Solution Explorer and selected Add => Existing Item. Then I navigated to the new physical files which I had just made and added them here.

Addresource 500X290

This added the resources to the solution. The next step was to reference these shared resources from each project which needed them. To do this I followed almost the same steps as before, create the solution folder in the web application project, then right click => Add => Existing Item and browse to where the resource is located (in the common folder which I created earlier). But this time instead of clicking Add, I clicked on the arrow next to the Add button and selected Add As Link.

Addresourceaslink 498X295

This adds a link to the resource rather than making a copy of the resource which is exactly what I wanted. You can tell if the resource is added as a link because it will have a small blue arrow in the corner of the icon which is the link icon.


Now I could build and publish my projects and the resources are included from the common location into each web application.

However there is one downside which is that when debugging the website these shared resources are not there. There is a trick to get this to work which I found on the blog of Matt Perdeck: I added a target to the .csproj file for each project that tells MSBuild to copy the shared resources on build, so that debugging will also work. I added the following code at the end of my .csproj file for each project:

<Target Name="CopyLinkedContentFiles" BeforeTargets="Build">
     <Copy SourceFiles="%(Content.Identity)" DestinationFiles="%(Content.Link)" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" Condition="'%(Content.Link)' != ''" />

It was simple for me with just a few steps to share resources between projects in my solution and adhere to the DRY (Don’t Repeat Yourself) philosophy and hopefully reduce the number of bugs in our project in the future.

Wir verwenden Cookies zur Bereitstellung von Website-Funktionen und zu Analysezwecken. Mehr zum Datenschutz