OPTEN, das einzige Umbraco-zertifizierte Unternehmen der Schweiz

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.Createsolutionfolder

Then I created physical folders which matched these solution folders. Createphysicalfolder

 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

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

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.

Linkedresourceicon

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: http://mattperdeck.com/post/Copying-linked-content-files-at-each-build-using-MSBuild.aspx. 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)' != ''" />
</Target>

 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.

http://auratel.ch/ma/astag?CheckReferer=true


kommentieren


1 Kommentar(e):

Mike Baker
Oktober 6, 2017 04:46

Thank you! For the past couple of days I have been trying to figure out how to share resources (specifically, images) between multiple projects in one VS solution - in my case desktop rather than web applications. An entire library of references books and hours of Googling found lots of grossly overcomplicated XAML and horrible approaches with shared dlls that simply did not work. Finally, you made it look easy! (Things usually are easy once you know how - it takes someone to show the way). Thanks again.