asp.net-mvc 您如何在一个解决方案中在多个项目之间共享脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8540292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you share scripts among multiple projects in one solution?
提问by EKet
In case the question wasn't clear. I have 3 MVC projects in one Solution. Every time I create a new project it adds the "Scripts" folder with all the .js files I'll ever need. I don't want to have this created every time for every application. Is there a way to reference scripts from a central folder in the solution so all applications/projects can share one common script folder with all the scripts common among them?
如果问题不清楚。我在一个解决方案中有 3 个 MVC 项目。每次我创建一个新项目时,它都会添加“Scripts”文件夹,其中包含我需要的所有 .js 文件。我不想每次都为每个应用程序创建这个。有没有办法从解决方案中的中央文件夹引用脚本,以便所有应用程序/项目可以共享一个通用脚本文件夹,其中包含所有通用脚本?
Edit:Please explain the pros and cons of doing this if there are any...now I'm curious.
编辑:如果有的话,请解释这样做的利弊......现在我很好奇。
回答by Erik Philips
Here is what I would recommend:
以下是我的建议:
Right click the solution and create a New Solution Foldercalled Common Javascript Files(or whatever you feel like calling it.
右键单击解决方案并创建一个名为的新解决方案文件夹Common Javascript Files(或您喜欢的任何名称。




Right click on the Solution, click Open Folder in Windows Explorer, or navigate there manually for other versions of Visual Studio :(
右键单击解决方案,单击在 Windows 资源管理器中打开文件夹,或手动导航到其他版本的 Visual Studio :(


In the solution directory, create a directory with the same name as the solution folder (solution folders do not normally match directories at the source code level but this will for sanity sake).
在解决方案目录中,创建一个与解决方案文件夹同名的目录(解决方案文件夹通常与源代码级别的目录不匹配,但为了安全起见,这将匹配)。


In this new directory, add files that need to be shared between solutions.
在这个新目录中,添加需要在解决方案之间共享的文件。


In Visual Studio, click the solution folder and select Add- Existing Item.
在 Visual Studio 中,单击解决方案文件夹并选择Add- Existing Item。


In the file selection dialog, navigate to the directory previous created, select the file(s) added to the directory and click Add.
在文件选择对话框中,导航到之前创建的目录,选择添加到目录中的文件,然后单击添加。




In each Project that needs a shared file, right click on the project (or directory within the project) and click Add- Existing Item.
在每个需要共享文件的项目中,右键单击该项目(或项目内的目录),然后单击Add- Existing Item。


Navigate to the shared Directory, Select the files and click the drop down arrowthen click Add As Link.
导航到共享目录,选择文件并单击下拉箭头,然后单击添加为链接。


Now the files in the projects are essentially short cuts to the files in the Solution Folder. But they are treated as actual files in the project (this includes .CS or Visual Basic files, they will be compiled as files that actually exist in the project).
现在,项目中的文件本质上是解决方案文件夹中文件的快捷方式。但它们被视为项目中的实际文件(这包括 .CS 或 Visual Basic 文件,它们将被编译为项目中实际存在的文件)。


PROS
优点
- Files are truly shared across projects at Design time
- Only the files needed for each project can be added, it's not all or nothing
- Does not require any configuration in IIS (virtual directory etc)
- 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 ANYfile you might need (Images, Css, Xml, CS, CSHTML, etc)
- 文件在设计时真正跨项目共享
- 只能添加每个项目需要的文件,不是全有或全无
- 不需要在 IIS 中进行任何配置(虚拟目录等)
- 如果解决方案在 TFS 源控制中,您可以将目录添加到 TFS 源,共享文件将受到源控制。
- 通过在项目中选择文件来编辑文件,将编辑实际文件。
- 删除链接文件不会删除文件。
- 这不仅限于 JS 文件,链接文件可以是您可能需要的任何文件(图像、Css、Xml、CS、CSHTML 等)
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.
- 每个部署都有自己的文件。
- 了解解决方案文件夹不是解决方案目录中存在的目录时,学习曲线很小。
回答by Ryan Mann
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"
最好的办法,imo,是推出你自己的 CDN...基本上只是在 IIS 中创建另一个站点并给它自己的绑定,例如“ 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.
然后将您所有的 css/js/fonts/共享图像等存储在 CDN 站点上,并从您的其他站点链接到它们。
Doing so solves 2 problems,
这样做解决了2个问题,
- All of your stuff is shared when it needs to be and you only have to manage 1 revision per file.
- Your users browsers can cache them in 1 single location instead of downloading copies of your stuff for every site that uses them..
- 您的所有内容都会在需要时共享,并且您只需管理每个文件的 1 个修订版。
- 您的用户浏览器可以将它们缓存在一个位置,而不是为每个使用它们的站点下载您的东西的副本。
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).
我添加了这个答案,因为我看到很多人在引用创建虚拟目录。虽然这确实共享文件,但它为它们创建了多个下载路径,这是对带宽的极大浪费。当您允许他们在 (cdn.somedomain.com) 上下载一次时,为什么要让您的用户下载 jquery.js(1 * 站点数)。
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.
此外,当我说带宽浪费时,我不只是在谈论服务器带宽,我在谈论数据计划中的移动用户......例如,我在手机上访问了我们公司的人力资源网站(保险等)一天,它一出门就消耗了 250MB,每个下载了 5 次 jquery 和一堆东西......在每月 2GB 的数据计划中,这样做的网站真的让我很恼火。
回答by formatc
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:
在这里,IMO 最好和最简单的解决方案,我花了一周的时间试图找到最好和最简单的方法,它总是比优点多:
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:
将以下内容添加到 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
以下是它的作用的解释:包括来自与 bin 目录相同级别的引用程序集的构建操作内容文件目录
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"
对 MvcApp2 执行相同操作。现在,每次构建后,新的静态文件都会被复制到您的应用程序中,您可以访问诸如“~/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:
如果你想你可以调整上面的命令来将脚本从 .dll 复制到每个应用程序的脚本文件夹,这样你就可以将一些脚本移动到 .dll 而无需更改任何路径,例如:
If you want to copy only scripts from Resources/Shared/scripts into MvcApp1/scripts after each build:
如果您只想在每次构建后将 Resources/Shared/scripts 中的脚本复制到 MvcApp1/scripts 中:
start xcopy "$(SolutionDir)Resources\Shared\Scripts\*" "$(SolutionDir)MvcApp1\Scripts" /r /s /i /y
回答by Ogglas
This is a late answer but Microsoft has added a project type called Shared Projectstarting Visual Studio 2013 Update 2that can do exactly what you wan't without having to linkfiles.
这是一个迟到的答案,但微软添加了一个名为Shared Project开始的项目类型Visual Studio 2013 Update 2,它可以做你想做的事,而无需link文件。
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."
“在以前版本的 Visual Studio 中,您可以通过添加 -> 现有项目然后选择链接来在项目之间共享源代码。但这有点笨拙,必须单独选择每个单独的源文件。随着支持多个不同的平台(iOS、Android 等),他们决定通过添加共享项目的概念来更轻松地在项目之间共享源代码。”
Info from this thread:
来自这个线程的信息:
What is the difference between a Shared Project and a Class Library in Visual Studio 2015?
Visual Studio 2015 中的共享项目和类库有什么区别?
回答by DivineOps
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 physicalfiles 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
- 选择一个“主”项目(您将用于调试)并将物理文件添加到其中
- 使用 Eric 的回答中所述的“添加为链接”功能将脚本文件添加到解决方案中的其他项目
- 按照 Mac 的评论中的建议,在 Build 上使用 CopyLinkedContentFiles 任务将文件复制到第二个到您的其他项目
This way you can modify the scripts in the "master" project without restarting the debugger, which to me makes the world of difference.
这样你就可以在不重启调试器的情况下修改“ master”项目中的脚本,这对我来说是不同的。
回答by Yuriy Faktorovich
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.
在 IIS 中,为 3 个应用程序中的每一个创建一个指向相同脚本文件夹的虚拟文件夹。然后您只需要将它们保存在一个应用程序中。还有其他选择,但这实际上取决于您的应用程序的结构。
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.
一个更可怕的想法是使用区域。在公共区域有一个脚本目录,其中包含要编译的脚本。然后通过将它们从 dll 中取出来自己提供它们。如果您预见到以后公共区域具有更多功能,这可能是一个好主意。
回答by jbrunken
Most of the files that are included by default are also available via various CDN's.
默认情况下包含的大多数文件也可以通过各种 CDN 获得。
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
微软的脚本 CDN:http: //www.asp.net/ajaxlibrary/cdn.ashx

