简体   繁体   English

.NET(Visual Studio)在项目之间共享资产

[英].NET (Visual Studio) Share assets between projects

I'm working with Visual Studio. 我正在使用Visual Studio。 There I have a solution with several web-projects (.net MVC 4). 在那里,我有一个包含多个web项目的解决方案(.net MVC 4)。 All of these web-projects use the same javascript-libs. 所有这些web项目都使用相同的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... “链接”来自另一个项目的javascript文件不是一个可能的解决方案,因为我必须链接数千个文件 (我正在使用的一个库是ExtJs)是什么使得无法在不冻结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. 目前我有一个名为“Web”的(Web)MVC项目和一个名为“ClientScript”的(类库)项目,它包含在多个Web项目之间共享的所有JavaScript文件。 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) . 由于链接所有需要的JavaScript文件不是一个可能的解决方案(因为它是导致visual studio冻结的数千个文件的问题) 我使用每个Web项目中的构建事件将所有需要的JavaScript文件复制到各个项目(项目 - >属性 - >构建事件 - >构建后) My Post-build command line in the Web Project looks like this: 我在Web项目中的Post-build命令行如下所示:

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. 每次构建Web项目时,所有已更改的Javascript文件都会从ClientScript项目复制到Web项目。

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. 在开发Javascripts时,我运行了一个小的“filewatcher”工具,当它发生变化时,它会自动将文件从ClientScript项目复制到每个Web项目。 This way I don't have to build the Web Project every time when I make a change to one of the Javascripts. 这样,每当我对其中一个Javascripts进行更改时,我都不必构建Web项目。

Place the JS files in a single folder, likely above all others, and add them to the project but use the "Link" option. 将JS文件放在一个文件夹中,可能高于其他文件夹,并将它们添加到项目中,但使用“链接”选项。 It's part of the drop down on the "OK" button in the "Add existing item..." dialog. 它是“添加现有项目...”对话框中“确定”按钮下拉菜单的一部分。

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. 任何在未来遇到此问题的人都应该知道Visual Studio中现在有共享项目来解决这个问题。 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 通用Windows项目默认使用它们,您可以通过下载和安装VS扩展来创建自己的项目: 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. 我创建了一个JavaScript共享项目,以在Windows商店js应用程序和MVC Web应用程序之间共享js文件,它不会让我这样说他们必须使用相同的语言。 (It supports C#, C++, or JavaScript). (它支持C#,C ++或JavaScript)。

When you run every new ASP.NET MVC 4 project it's take a new port then other app have take. 当您运行每个新的ASP.NET MVC 4项目时,它需要一个新的端口,然后其他应用程序。

I simply suggest you a simple thing. 我只是建议你一个简单的事情。

run a project which contain all the pacakages. 运行一个包含所有pacakages的项目。 open them webmatrix and run them as localhost:80. 打开它们webmatrix并将它们作为localhost:80运行。

You need to set the port in settings section of your site in webmatrix. 您需要在webmatrix中设置站点的设置部分中的端口。 Now it will rechable at localhost now you can reference all the libraries from this packages. 现在它将在localhost上重新生成,现在您可以引用此包中的所有库。

Slightly older thread, but I have another way of doing a similar thing using Web Essentials, that handles the issue of not publishing correctly. 稍微老一点的线程,但我有另一种使用Web Essentials做类似事情的方法,它处理不能正确发布的问题。

I have a shared folder outside of the projects that require the shared file, normally a 'common' project with other things in as well, but can be just a simple folder as suggested by Michael Perrenoud. 我在需要共享文件的项目之外有一个共享文件夹,通常也是一个“常见”项目,但也可以是Michael Perrenoud建议的一个简单文件夹。

However instead of 'Add as Link' I have been creating a new bundle in the project that requires the shared js/css file, with the same name as the shared file, and then referencing that file in the shared folder using a relative reference location rather than the root based one it starts with. 但是,我在项目中创建了一个新的包,而不是“添加为链接”,它需要共享的js / css文件,其名称与共享文件相同,然后使用相对引用位置在共享文件夹中引用该文件而不是它开始的根基。

To add a file from a shared folder in the root of the solution to the scripts folder use the following code in a new bundle file (*.bundle), changing the folder/file names as required. 要将解决方案根目录中的共享文件夹中的文件添加到脚本文件夹,请在新的捆绑文件(* .bundle)中使用以下代码,根据需要更改文件夹/文件名。

<?xml version="1.0" encoding="utf-8"?>
<bundle xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://vswebessentials.com/schemas/v1/bundle.xsd">
        <settings>
                <minify>false</minify>
                <runOnBuild>true</runOnBuild>
        </settings>
        <files>
                <file>../../MySharedFolder/my-js-file.js</file>
        </files>
</bundle>

Then every time you build it recreates the bundle with the latest version, this version is then also published as expected :) 然后每次构建它都会重新创建具有最新版本的包,然后该版本也会按预期发布:)

You can even create a minified version if desired by changing 'minify' to true. 如果需要,您甚至可以通过将“minify”更改为true来创建缩小版本。 Or better yet you can add them loads as a bundle too if you want, you have that flexibilty. 或者更好的是,如果你愿意,你也可以将它们作为一个包加载,你有灵活性。

This is an older thread but due to complex business requirements these days applications are divided in to different modules or sub projects.Thus, brings us the need to share common resources like JavaScript files, themes and CSS style sheet files. 这是一个较旧的线程,但由于复杂的业务需求,现在应用程序分为不同的模块或子项目。因此,我们需要共享JavaScript文件,主题和CSS样式表文件等公共资源。

I personally feel that common files should be put in separate Asp .Net MVC 5 project which has following structure : ASP.NET MVC5 folder structure 我个人认为常见的文件应放在单独的Asp .Net MVC 5项目中,该项目具有以下结构: ASP.NET MVC5文件夹结构

在此输入图像描述

Now the best part is you can separately manage the dependencies using Bower,NPM or Nuget package manager. 现在最好的部分是您可以使用Bower,NPM或Nuget包管理器单独管理依赖项。

After you have organised all the files in this project host this project to your own CDN or may be on cloud. 组织完此项目中的所有文件后,将此项目托管到您自己的CDN或可能在云上。 You can use Using CDN in Bundle Approach to get script or link references. 您可以使用捆绑方法中的使用CDN来获取脚本或链接引用。

That will help you sharing common resources across all the projects.There us a short coming though if you have many developers on the team and if someone added incompatible version lib can affect all the apps. 这将有助于您在所有项目中共享公共资源。如果团队中有许多开发人员,并且如果有人添加了不兼容的版本库,则会影响所有应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM