How do you share scripts among several projects in one solution?

If the question is not clear. I have 3 MVC projects in one solution. Each time I create a new project, it adds a โ€œScriptsโ€ folder with all the .js files that I will ever need. I do not want this to be created every time for every application. Is there any way to refer to scripts from the central folder in the solution, so all applications / projects can share the same shared script folder with all the scripts common among them?

Edit: Please explain the pros and cons of this, if any ... now I'm curious.

+59
asp.net-mvc asp.net-mvc-3
Dec 16 '11 at 9:21
source share
6 answers

Here is what I would recommend:

Right-click on the solution and create a new solutions folder called Common Javascript Files (or whatever you want to name.

New solution folder

Common Javascript Files Solution Folder

Right-click on Solution, select Open Folder in Windows Explorer, or go there manually for other versions of Visual Studio: (

Open Folder In Windows Explorer

In the solutions directory, create a directory with the same name as the solution folder (solution folders usually do not match the directories at the source code level, but this will be useful for the sled).

Common Javascript Files Directory

In this new directory, add the files that need to be shared between the solutions.

Add Javascript Files To Directory

In Visual Studio, select the solutions folder and select Add - Existing Item.

Visual Studio Add - Existing Itme

In the file selection dialog box, go to the directory created earlier, select the files added to the directory, and click "Add."

Select Files To Add

Solution Folder Files

In each project that requires a shared file, right-click the project (or directory in the project) and click Add - Existing Item.

Project Add Existing Item

Go to the shared directory, select the files and click the drop-down arrow , then click "Add as link."

Add as link

Now files in projects are significantly reduced to files in the Solution folder. But they are considered as actual files in the project (including .CS or Visual Basic files, they will be compiled as files that really exist in the project).

Linked Files

PROFI

  • Files truly available for various projects during development
  • You can add only the files needed for each project, itโ€™s not all or nothing.
  • No configuration required in IIS (virtual directory, etc.)
  • If the solution is in the TFS Source control, you can add the directory to the TFS source and the shared files will be controlled by the source code.
  • Editing a file by selecting it in the project will edit the actual file.
  • Deleting a linked file does not delete the file.
  • These are not only JS files, related files can be ANY file you may need (images, Css, Xml, CS, CSHTML, etc.)

Cons

  • Each deployment gets its own file.
  • There is a small learning curve with the understanding that solution folders are not directories that exist in the solution catalog.
+90
Dec 17 2018-11-12T00:
source share

The best thing to do imo is to collapse your own CDN ... Basically just create another site in IIS and give it a binding, for example. " http://cdn.somedomain.com "

Then save all your css / js / fonts / shared images etc. on the CDN website and connect to them from other sites.

As a result, 2 problems are solved,

  • All your materials are available when necessary, and you only need to manage one version for each file.
  • Custom browsers can cache them in one place, and not download copies of your materials for each site that uses them.

I added this answer because I see that many people refer to the creation of virtual directories. Although it does share files, it does create several download paths for them, which is a waste of bandwidth. Why should users download jquery.js (1 * number of sites) when you can allow them to download it once (cdn.somedomain.com).

Also, when I talk about the lack of bandwidth, Iโ€™m not only talking about the bandwidth of the server, Iโ€™m talking about mobile users according to data plans ... For example, I hit our HR website (insuance, etc.) on my website phone another day, and he consumed 250 MB right outside the gate, downloaded jquery and a bunch of things 5 โ€‹โ€‹times each ... On a 2gb plan per month, sites that really annoy me.

+19
Dec 22 '14 at 5:43
source share

Here it is, IMO - the best and easiest solution, I spent a week trying to find the best and easiest way, which always had more disadvantages than professionals:

 Resources(DLL) Shared images image.png css shared.css scripts jquery.js MvcApp1 Images Content Shared <- We want to get files from above dll here ... MvcApp2 Images Content Shared <- We want to get files from above dll here ... 

Add the following to MvcApp1 -> Project -> MvcApp1 Properties -> Build events -> post build event:

 start xcopy "$(SolutionDir)Resources\Shared\*" "$(SolutionDir)MvcApp1\Shared" /r /s /i /y 

Here's an explanation of what it does: Including the directory for the contents of the assembly contents files from the referenced assembly at the same level as the bin directory

Do the same for MvcApp2. Now after each build, fresh static files will be copied to your application, and you will be able to access files like "~ / Shared / css / site.css"

If you want, you can configure the above command to copy scripts from .dll to the script folder of each application, so you could move some scripts to .dll without changing any paths, here is an example:

If you want to copy only scripts from / Shared / scripts resources to MvcApp1 / scripts after each build:

 start xcopy "$(SolutionDir)Resources\Shared\Scripts\*" "$(SolutionDir)MvcApp1\Scripts" /r /s /i /y 
+10
May 26 '13 at 19:32
source share

In IIS, create a virtual folder that points to the same scripts folder for each of the three applications. Then you need to save them in only one application. There are other alternatives, but it depends on how your applications are structured.

Edit

A worse idea is to use the Regions. In the common area there is a script directory with scripts installed. Then service them yourself, pulling them out of the DLL. It might be a good idea if you provide for a common area that has more functionality later.

+2
Dec 16 '11 at 21:25
source share

A suggestion that allows you to debug your scripts without recompiling the project:

  • Select one master project "(which you will use for debugging) and add physical files to it
  • Use the "Add as link" function as described in Eric to add script files to other projects in the solution.
  • Use the CopyLinkedContentFiles task to build, as suggested in a Mac comment, to copy files to the second in your additional projects.

This way you can change the scripts in the master project without restarting the debugger, which for me makes a world of difference.

+2
Feb 23 '15 at 22:19
source share

Most files that are included by default are also available through various CDNs.

If you do not add your own scripts, you might not even need a script directory.

Microsoft CDN for scripts: http://www.asp.net/ajaxlibrary/cdn.ashx

+1
Dec 16 '11 at
source share



All Articles