简体   繁体   English

在Visual Studio 2013中为Web资产创建项目

[英]Creating a project for web assets in Visual Studio 2013

I have multiple ASP.NET MVC5 projects that use several similar styles/scripts, and I think, for maintenance's sake, it'd be ideal to have a separate project in Visual Studio 2013 containing the LESS stylesheets and JavaScript files that can be shared between the two. 我有多个使用几种相似样式/脚本的ASP.NET MVC5项目,并且我认为,出于维护的目的,在Visual Studio 2013中拥有一个单独的项目(包含可在以下项目之间共享的LESS样式表和JavaScript文件)是理想的选择他们俩。

My setup is as follows: 我的设置如下:

Web.Project1
Web.Project2
Web.Assets //would contain LESS/JS files
Resources
Domain
Data

Any pointers? 有指针吗?

I've been looking at adding as a link, but I'm not certain that would accomplish what I need. 我一直在寻找添加为链接,但不确定是否可以满足我的需要。

I recently went through this process for a project of mine, and concluded that if your goal is simply to avoid duplication by keeping your code DRY then using linked files works great. 我最近在一个我的项目中经历了这个过程,得出的结论是,如果您的目标只是通过使代码保持DRY来避免重复,那么使用链接文件非常有用。

It's simple enough to do, but I'll write it up anyway as it might save someone some time. 这很简单,但是无论如何我都会写出来,因为这可能会节省一些时间。

Quick Summary 快速总结

  1. Create common Solution Folders containing the resources to be shared, or simply designate one of the projects to be the master. 创建包含要共享资源的公共解决方案文件夹 ,或者简单地将其中一个项目指定为主文件。
  2. Use "Add as Link" to add the shared resource files to each project as needed. 根据需要使用“添加为链接”将共享资源文件添加到每个项目。
  3. Add an AfterBuild task to each project file that will copy the linked files to project folders. 向每个项目文件添加一个AfterBuild任务,该任务会将链接的文件复制到项目文件夹。 This is only needed so that Visual Studio test/debug (F5) will work locally. 只有这样做才能使Visual Studio测试/调试(F5)在本地运行。

Detailed explanation follows: 详细说明如下:

Configuring Solution Folders for the Shared Resources 为共享资源配置解决方案文件夹

** Note that if you're simply going to share files directly from one project to one or more additional projects then you can skip this step. ** 请注意,如果您只是直接将文件从一个项目直接共享到一个或多个其他项目,则可以跳过此步骤。

Visual Studio solution folders do not have to reflect physical file system folders, but doing so will help preserve your sanity. Visual Studio解决方案文件夹不必反映物理文件系统文件夹,但这样做将有助于保持理智。 So first create the folders on your local file system and copy the resource files into them. 因此,首先在本地文件系统上创建文件夹,然后将资源文件复制到其中。 The new folders should be located under your solution folder. 新文件夹应位于解决方案文件夹下。 For example: 例如:

\MySolution
   \Common
      \Images
      \Scripts
      \Styles

Back in Visual Studio, right click on the Solution Items folder and use Add Solution Folder to replicate the new file system folders. 返回Visual Studio,右键单击“ 解决方案项目”文件夹,然后使用“ 添加解决方案文件夹”复制新的文件系统文件夹。

Next, add the files to the new solution folders by right-clicking each folder and using Add Existing Item to add the contents of the folders. 接下来,通过右键单击每个文件夹并使用“ 添加现有项”添加文件夹的内容,将文件添加到新的解决方案文件夹中。

Add Shared Resources as Links 将共享资源添加为链接

For each project that will use the shared resources, right-click the project folder and choose Add Existing Item . 对于将使用共享资源的每个项目,右键单击项目文件夹,然后选择添加现有项 Browse to the common folder, select the desired files, click the drop-down arrow next to the "Add" button and choose "Add as Link". 浏览到公共文件夹,选择所需的文件,单击“添加”按钮旁边的下拉箭头,然后选择“添加为链接”。

You may get a source control warning about adding files that are outside of the project directory structure, but this can be ignored since the linked file will be under source control at its source. 您可能会收到有关添加项目目录结构之外的文件的源代码控制警告,但是由于链接文件将在其源代码下受到源代码管理,因此可以忽略此警告。

Add an AfterBuild Task to Copy Files 添加AfterBuild任务以复制文件

When you publish the application to a server the linked files will copied to the project folders to which they are linked and everything works as expected. 当您将应用程序发布到服务器时,链接的文件将复制到链接到的项目文件夹中,并且一切正常。 However, in the development environment the linked files do not physically reside in the project folders. 但是,在开发环境中,链接文件实际上并不驻留在项目文件夹中。 So when you hit F5 to test your application in VS, the shared resources will be missing. 因此,当您按F5键以在VS中测试您的应用程序时,共享资源将丢失。

The simple solution is to add an MSBuild task to copy the linked files from their source after each build. 简单的解决方案是添加一个MSBuild任务,以便在每次构建后从其源文件中复制链接的文件。 This needs to be done to for each project that contains the shared resource links. 对于包含共享资源链接的每个项目,都需要这样做。

Right-click the project and choose Unload Project . 右键单击项目,然后选择卸载项目 Right-click the project again and choose Edit <ProjectFileName> . 再次右键单击该项目,然后选择Edit <ProjectFileName> Scroll to the bottom and add the following (just above "</Project>"): 滚动到底部并添加以下内容(恰好在“ </ Project>”上方):

  <Target Name="AfterBuild">
    <!-- Copy linked content files to local folders so that they are available in the debugger. 
         This is only an issue when running the application locally. The linked files should 
         be automatically published to the correct folder when publishing to a web server. -->
    <Copy SourceFiles="%(Content.Identity)"
          DestinationFiles="%(Content.Link)"
          SkipUnchangedFiles='true'
          OverwriteReadOnlyFiles='true'
          Condition="'%(Content.Link)' != ''" />
  </Target>

** Copy task adapted from this link . ** 复制从此链接改编的任务。

Save the project file then right-click and choose Reload Project . 保存项目文件,然后右键单击并选择Reload Project

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

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