Visual Studio“添加为链接"调试时不工作 [英] Visual Studio "Add As Link" not working while debugging

查看:24
本文介绍了Visual Studio“添加为链接"调试时不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Visual Studio 2010 来维护大约 40 个不同的 Web 应用程序,这些应用程序在部署时都位于同一个网站上.这些项目在同一个解决方案中,但位于不同的项目中,因为它们每个都做不同的事情.

I use Visual Studio 2010 to maintain around 40 different web applications that are all housed on the same website upon deployment. These projects are in the same solution but housed in different projects as they each do very different things.

我正在尝试通过添加为链接"选项在各个项目之间共享 css/js 文件,这样我就可以更新 css 或 js 文件一次,并自动将更新反映在各个项目中,而无需构建并重新部署每个项目.

I'm trying to share css/js files among the various projects via the "Add As Link" option that way I can update the css or js files once and automatically have the updates reflected in the various projects without having to build and redeploy each project.

我遇到的问题是,当我尝试在我的 PC 上本地运行项目以进行调试时,这些链接文件不起作用.找不到文件.

The issue that I am having is when I am trying to run a project locally on my PC for debugging purposes those linked files aren't working. I'm getting a file not found.

我的想法:我认为这是因为在本地运行应用程序时文件夹结构不同.我很好奇是否有一种方法可以仅在调试模式下构建时将文件复制到项目并相应地调整相对 URL,以便我能够在发布到我们的网络服务器之前正确测试应用程序.

My Thought: I assume that this because the folder structure is different when running the applications locally. I'm curious if there is a way to copy the files to the project only when building in debug mode and adjust the relative URLs accordingly so that I will be able to properly test the application before publishing to our web server.

谢谢,

推荐答案

此问题的解决方案是复制在每次构建期间作为链接添加的内容文件(如 js、css 或其他).有几种方法可以做到这一点.我可以建议使用 MSBuild 目标,它可以被不同的 Web 应用程序项目重用.

The solution to this problem is copying content files (like js, css or others) which are added as link during each build. There are several ways to do this. I can advice to use MSBuild target which can be reused by different web application projects.

因此您可以使用以下内容创建以下文件(例如,将其命名为 WebApplication.Extension.targets):

So you can create the following file (for example by naming it WebApplication.Extension.targets) with the following content:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <!-- Override the default target dependencies to -->
    <!-- include the new CopyLinkedContentFiles target. -->
    <PropertyGroup>
        <BuildDependsOn>
            CopyLinkedContentFiles;
            $(BuildDependsOn);
        </BuildDependsOn>
    </PropertyGroup>

    <!--
    ============================================================
    CopyLinkedContentFiles

    A new target to copy any linked content files into the 
    web application output folder.

    NOTE: This is necessary even when '$(OutDir)' has not been redirected.
    ============================================================
    -->
    <Target Name="CopyLinkedContentFiles">
        <!-- Remove any old copies of the files -->
        <Delete Condition=" '%(Content.Link)' != '' AND Exists('$(WebProjectOutputDir)\%(Content.Link)') "
                Files="$(WebProjectOutputDir)\%(Content.Link)" />
        <!-- Copy linked content files recursively to the project folder -->
        <Copy Condition=" '%(Content.Link)' != '' " SourceFiles="%(Content.Identity)"
              DestinationFiles="$(WebProjectOutputDir)\%(Content.Link)" />
    </Target>
</Project>

然后通过在 .csproj 文件中放置以下行,将此目标添加到您的 Web 应用程序项目中:

And then add this target to you web application project by placing the following line in .csproj file:

<Import Project="$(MSBuildProjectDirectory)[RelativePathToFile]WebApplication.Extension.targets" />

基本上你可以在 .csproj 文件中添加以下一行:

Basically you can add this line in .csproj file after the following one:

<Import Project="$(MSBuildBinPath)Microsoft.CSharp.targets" />

在添加为链接的内容文件的问题之后,应该可以解决.

After this problem with content files added as link should be resolved.

要仅在 MSBUILD 中构建调试配置时执行以下逻辑,您可以指定 Condition 元素.

To execute the following logic only when debug configuration is built in MSBUILD you have ability to specify Condition element.

例如为了只为调试配置导入指定的目标,您可以将导入语句更新为以下内容:

For example to import specified target only for debug configuration you can update import statement to the following:

<Import Project="$(MSBuildProjectDirectory)[RelativePathToFile]WebApplication.Extension.targets" Condition=" '$(Configuration)' == 'Debug' "/>

编辑 2:

为了解决这个问题,前段时间我创建了一个 'MSBuild.WebApplication.CopyContentLinkedFiles' nuget 包.此包添加了 MsBuild 目标,该目标将在构建期间作为链接添加到项目文件夹的所有内容文件复制.

To overcome this problem some time ago I created a 'MSBuild.WebApplication.CopyContentLinkedFiles' nuget package. This package adds MsBuild target which copies all content files added as link to project folder during build.

这篇关于Visual Studio“添加为链接"调试时不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆