Here is a “dead simple” solution for sharing web resources between projects without using CDN, LESS, SASS, NuGet, etc.:
- Create shared solution folders containing shared resources, or simply designate one of the projects as a wizard.
- Use Add As Link to add shared resources files to each project as needed.
- Add an AfterBuild task to each project file that will copy related files to the project folders. This is only necessary for Visual Studio test / debug (F5) to work locally.
If you need information on how to do this, read on.
Configure solution folders for shared resources
** Please note that if you are just going to share files directly from one project with one or more additional projects, you can skip this step.
Visual Studio solution folders should not reflect folders on physical file systems, but this will help preserve your sanity. Therefore, first create folders on the local file system and copy resource files to them. New folders should be located under the folder of your solution. For example:
\MySolution \Common \Images \Scripts \Styles
Return to Visual Studio, right-click the Solution Items folder and use the Add Solution Folder to replicate the new file system folders.
Then add the files to the new solution folders by right-clicking on each folder and using Add existing item to add the contents of the folders.
Add Shares as Links
For each project that will use shared resources, right-click the project folder and select "Add Existing Item." Go to the shared folder, select the files you want, click the drop-down arrow next to the Add button, and select Add As Link.
You may receive a source control warning about adding files that are outside the project directory structure, but this can be ignored, since the linked file will be under source control in its source.
Add AfterBuild task to copy files
When you publish the application on the server, the related files will be copied to the project folders to which they are attached, and everything works as expected. However, in the development environment, related files are not physically located in the project folders. Therefore, when you press F5 to test your application in VS, there will be no shared resources.
A simple solution is to add an MSBuild task to copy related files from their source after each build. This must be done for each project that contains shared resource references.
Right-click the project and select Upload Project. Right-click the project again and select Edit <ProjectFileName>. Scroll down the page and add the following (just above "</Project>"):
<Target Name="AfterBuild"> <Copy SourceFiles="%(Content.Identity)" DestinationFiles="%(Content.Link)" SkipUnchangedFiles='true' OverwriteReadOnlyFiles='true' Condition="'%(Content.Link)' != ''" /> </Target>
** Copy the task adapted from this link in TheCodeDestroyer answer.
Save the project file, then right-click and select Update Project.