加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – Visual Studio“添加为链接”调试时不工作

发布时间:2020-12-15 18:47:52 所属栏目:asp.Net 来源:网络整理
导读:我使用Visual Studio 2010来维护大约40个不同的web应用程序,它们都部署在同一个网站上。这些项目在同一个解决方案中,但是它们位于不同的项目中,因为它们各有不同。 我正在尝试通过“添加为链接”选项在各种项目之间共享css / js文件,以便我可以更新css或
我使用Visual Studio 2010来维护大约40个不同的web应用程序,它们都部署在同一个网站上。这些项目在同一个解决方案中,但是它们位于不同的项目中,因为它们各有不同。

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

我遇到的问题是当我试图在我的电脑上在本地运行一个项目以进行调试时,这些链接的文件不起作用。我正在找到一个没有找到的文件。

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或其他)。有几种方法可以做到这一点。我可以建议使用可以由不同的Web应用程序项目重用的MSBuild目标。

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

<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应用程序项目中:

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

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

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

此问题后,内容文件添加为链接应该被解决。

编辑:
仅当在MSBUILD中内置调试配置时,才能执行以下逻辑,您可以指定Condition元素。

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

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

编辑2:

为了克服这个问题,前段时间我创建了一个’MSBuild.WebApplication.CopyContentLinkedFiles‘nuget包。此程序包添加MsBuild目标,该目标在构建过程中将所有添加到链接的内容文件复制到项目文件夹。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读