调试时,Visual Studio"添加为链接"不起作用

Cha*_*tah 26 asp.net webforms visual-studio-2010 asp.net-mvc-3 web

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

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

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

我的想法:我认为这是因为在本地运行应用程序时文件夹结构不同.我很好奇是否有办法只在构建调试模式时将文件复制到项目并相应地调整相对URL,这样我才能在发布到我们的Web服务器之前正确测试应用程序.

谢谢,

Max*_*lov 34

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

因此,您可以使用以下内容创建以下文件(例如,通过命名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>
Run Code Online (Sandbox Code Playgroud)

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

<Import Project="$(MSBuildProjectDirectory)[RelativePathToFile]\WebApplication.Extension.targets" />
Run Code Online (Sandbox Code Playgroud)

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

<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
Run Code Online (Sandbox Code Playgroud)

此问题与内容文件添加为链接后应解决.

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

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

<Import Project="$(MSBuildProjectDirectory)[RelativePathToFile]\WebApplication.Extension.targets" Condition=" '$(Configuration)' == 'Debug' "/>
Run Code Online (Sandbox Code Playgroud)

EDIT2:

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

  • @MaximKornilov喜欢你用NuGet包回答了一个帖子......就在那里,OP提供了jsbin网站问题样本;) (2认同)

use*_*862 30

使用MSBuild在每个构建中自动将所有链接的内容文件复制到其"虚拟"位置的解决方案如下:

http://mattperdeck.com/post/Copying-linked-content-files-at-each-build-using-MSBuild.aspx

这可以确保在您按F5时,您的链接文件可供浏览器使用.


Diz*_*zle 6

也可以通过将以下代码段粘贴到包含链接文件的 .proj 文件末尾来解决此问题

<Target Name="CopyLinkedContentFiles" BeforeTargets="Build">
<Copy SourceFiles="%(Content.Identity)" 
      DestinationFiles="%(Content.Link)" 
      SkipUnchangedFiles='true' 
      OverwriteReadOnlyFiles='true' 
      Condition="'%(Content.Link)' != ''" />
Run Code Online (Sandbox Code Playgroud)