program tip

하나의 솔루션에서 여러 프로젝트간에 스크립트를 어떻게 공유합니까?

radiobox 2020. 10. 29. 07:59
반응형

하나의 솔루션에서 여러 프로젝트간에 스크립트를 어떻게 공유합니까?


질문이 명확하지 않은 경우. 하나의 솔루션에 3 개의 MVC 프로젝트가 있습니다. 새 프로젝트를 만들 때마다 필요한 모든 .js 파일이 포함 된 "Scripts"폴더가 추가됩니다. 모든 응용 프로그램에 대해 매번 생성되는 것을 원하지 않습니다. 솔루션의 중앙 폴더에서 스크립트를 참조하여 모든 애플리케이션 / 프로젝트가 공통 스크립트 폴더 하나를 공통 스크립트 폴더와 공유 할 수있는 방법이 있습니까?

편집 : 이 작업의 장단점을 설명해주세요 ... 이제 궁금합니다.


내가 추천하는 것은 다음과 같습니다.

솔루션을 마우스 오른쪽 버튼으로 클릭하고 라는 새 솔루션 폴더를 만듭니다 Common Javascript Files(또는 원하는대로 호출합니다.

새 솔루션 폴더

공통 Javascript 파일 솔루션 폴더

솔루션을 마우스 오른쪽 버튼으로 클릭 하고 Windows 탐색기에서 폴더 열기를 클릭 하거나 다른 버전의 Visual Studio를 위해 수동으로 탐색합니다.

Windows 탐색기에서 폴더 열기

솔루션 디렉터리에서 솔루션 폴더와 이름이 같은 디렉터리를 만듭니다 (솔루션 폴더는 일반적으로 소스 코드 수준의 디렉터리와 일치하지 않지만 이것은 온전함을위한 것입니다).

공통 자바 스크립트 파일 디렉토리

이 새 디렉토리에서 솔루션간에 공유해야하는 파일을 추가하십시오.

디렉토리에 자바 스크립트 파일 추가

Visual Studio에서 솔루션 폴더를 클릭하고 추가 - 기존 항목을 선택 합니다.

Visual Studio 추가-기존 Itme

파일 선택 대화 상자에서 생성 이전 디렉토리로 이동은, 디렉토리에 추가 파일을 선택하고 클릭 추가 .

추가 할 파일 선택

솔루션 폴더 파일

공유 파일이 필요한 각 프로젝트에서 프로젝트 (또는 프로젝트 내의 디렉토리)를 마우스 오른쪽 버튼으로 클릭하고 추가 - 기존 항목을 클릭 합니다.

프로젝트 기존 항목 추가

공유 디렉토리로 이동하여 파일을 선택 하고 드롭 다운 화살표 를 클릭 한 다음 링크로 추가 를 클릭 합니다.

링크로 추가

이제 프로젝트의 파일은 본질적으로 솔루션 폴더의 파일에 대한 바로 가기입니다. 그러나 이들은 프로젝트에서 실제 파일로 취급됩니다 (여기에는 .CS 또는 Visual Basic 파일이 포함되며 프로젝트에 실제로 존재하는 파일로 컴파일됩니다).

링크 된 파일

장점

  • 파일은 디자인 타임에 프로젝트간에 공유 됩니다.
  • 각 프로젝트에 필요한 파일 만 추가 할 수 있습니다. 전부 또는 전부는 아닙니다.
  • IIS (가상 디렉터리 등)의 구성이 필요하지 않습니다.
  • If the solution is in TFS Source control, you can add the Directory to the TFS Source and the shared files will be source controlled.
  • Editing a file by selecting it in the Project, will edit the actual file.
  • Deleting a Linked file does not delete the file.
  • This is not limited to JS files, linked files can be ANY file you might need (Images, Css, Xml, CS, CSHTML, etc)

CONS

  • Each deployment gets it's own file.
  • There is a small learning curve when understanding that Solution Folders are not Directories that exist in a Solution Directory.

The best thing to do, imo, is to roll your own CDN... Basically just create another site in IIS and give it it's own binding, e.g. "http://cdn.somedomain.com"

Then store all of your css/js/fonts/shared images etc on the CDN site and link to them from your other sites.

Doing so solves 2 problems,

  1. All of your stuff is shared when it needs to be and you only have to manage 1 revision per file.
  2. Your users browsers can cache them in 1 single location instead of downloading copies of your stuff for every site that uses them..

I added this answer because I see a lot of people referrencing creating virtual directories. While that does indeed share the files, it creates multiple download paths for them which is an extreme waste of bandwidth. Why make your users download jquery.js (1 * number of sites) when you can allow them to download it once on (cdn.somedomain.com).

Also when I say waste of bandwidth, I'm not just talking about server bandwidth, I'm talking about mobile users on data plans... As an example, I hit our companies HR site (insuance etc) on my phone the other day and it consumed 250mb right out the gate, downloaded jquery and a bunch of stuff 5 times each... On a 2gb a month data plan, websites that do that really annoy me.


Here it goes, IMO the best and easiest solution, I spent a week trying to find best and easiest way which always had more cons than pros:

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 following to MvcApp1 -> Project -> MvcApp1 Properties -> Build events -> post build event:

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

Here is explanation on what it does: Including Build action content files directory from referenced assembly at same level as bin directory

Do the same for MvcApp2. Now after every build fresh static files will be copied to your app and you can access files like "~/Shared/css/site.css"

If you want you can adjust the above command to copy scripts from .dll to scripts folder of every app, that way you could move some scripts to .dll without having to change any paths,here is example:

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

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

This is a late answer but Microsoft has added a project type called Shared Project starting Visual Studio 2013 Update 2 that can do exactly what you wan't without having to link files.

The shared project reference shows up under the References node in the Solution Explorer, but the code and assets in the shared project are treated as if they were files linked into the main project.

"In previous versions of Visual Studio, you could share source code between projects by Add -> Existing Item and then choosing to Link. But this was kind of clunky and each separate source file had to be selected individually. With the move to supporting multiple disparate platforms (iOS, Android, etc), they decided to make it easier to share source between projects by adding the concept of Shared Projects."

https://blogs.msdn.microsoft.com/somasegar/2014/04/02/visual-studio-2013-update-2-rc-windows-phone-8-1-tools-shared-projects-and-universal-windows-apps/

Info from this thread:

What is the difference between a Shared Project and a Class Library in Visual Studio 2015?

https://stackoverflow.com/a/30638495/3850405


In IIS create a virtual folder pointing to the same scripts folder for each of the 3 applications. Then you'll only need to keep them in a single application. There are other alternatives, but it really depends on how your applications are structured.

Edit

A scarier idea is to use Areas. In a common area have a scripts directory with the scripts set to be compiled. Then serve them up yourself by getting them out of the dll. This might be a good idea if you foresee the common Area having more functionality later.


A suggestion that will allow you to debug your scripts without re-compiling the project:

  • Pick one "master" project (which you will use for debugging) and add the physical files to it
  • Use "Add As Link" feature as described in Eric's answer to add the script files to the other projects in solution
  • Use CopyLinkedContentFiles task on Build, as suggested in Mac's comment to copy the files over to the second over to your additional projects

This way you can modify the scripts in the "master" project without restarting the debugger, which to me makes the world of difference.


Most of the files that are included by default are also available via various CDN's.

If you're not adding your own custom scripts, you may not even need a scripts directory.

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

참고 URL : https://stackoverflow.com/questions/8540292/how-do-you-share-scripts-among-multiple-projects-in-one-solution

반응형