Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET (Visual Studio) Share assets between projects

I'm working with Visual Studio. There I have a solution with several web-projects (.net MVC 4). All of these web-projects use the same javascript-libs. Currently I copied the library into each project, but this can't be the final solution. What is the best approach to share those library (assets in general) between all of the projects? Just link them? Or is it possible to create a project and reference it in all projects?

Update

"Link" the javascript files from another project is not a possible solution as I would have to link thousands of files (one of the libraries I am using is ExtJs) what makes it impossible to build a project without freezing visual studio...

Possible solution

Currently I have a (Web) MVC Project called "Web" and a (Class Library) Project called "ClientScript" which contains all the JavaScript files which are shared between several Web Projects. As linking all the needed JavaScript files is not a possible solution (because it's a matter of thousands of files what causes visual studio to freeze) I copy all the needed JavaScript files to the individual Projects using the Build Events in each Web Project (Project -> Properties -> Build Events -> Post-build). My Post-build command line in the Web Project looks like this:

start xcopy "$(SolutionDir)ClientScript\Frontend\*" "$(SolutionDir)Web\Scripts" /r /s /i /y /D /E

Every time you build your Web Project all the changed Javascript files get copied from the ClientScript Project to your Web Project.

While developing the Javascripts I run a small "filewatcher" tool which automatically copies a file from the ClientScript Project to every Web Project when it changes. This way I don't have to build the Web Project every time when I make a change to one of the Javascripts.

like image 722
sjkm Avatar asked Nov 18 '13 13:11

sjkm


People also ask

Can two projects reference each other C#?

This can be achieved by adding reference of one project in the other project. If both the projects are in the same solution, then expand the project name, right click on 'References', click on Add references. Go to 'Projects' Tab, select the project name which you want to use in current project, click ok.

What is shared project in Visual Studio?

Shared Projects let you write common code that is referenced by a number of different application projects. The code is compiled as part of each referencing project and can include compiler directives to help incorporate platform-specific functionality into the shared code base.


Video Answer


2 Answers

Place the JS files in a single folder, likely above all others, and add them to the project but use the "Link" option. It's part of the drop down on the "OK" button in the "Add existing item..." dialog.

like image 77
Mike Perrenoud Avatar answered Sep 27 '22 20:09

Mike Perrenoud


Anyone that stumbles across this question here in the future should know that there are now Shared Projects in Visual Studio to solve this problem. Universal Windows projects use them by default and you can create your own by downloading and installing the VS extension here: https://visualstudiogallery.msdn.microsoft.com/315c13a7-2787-4f57-bdf7-adae6ed54450

Note: At this time they can be picky about what type of project you try to add the shared reference. I created a JavaScript shared project to share js files between a Windows store js app and an MVC web app and it would not let me do that saying they had to be of the same language. (It supports C#, C++, or JavaScript).

like image 35
Eric Wardell Avatar answered Sep 27 '22 21:09

Eric Wardell