asp.net - Visual Studio "Add As Link"在调试时不起作用

标签 asp.net asp.net-mvc-3 visual-studio-2010 web webforms

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

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

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

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 应用程序项目重用。

因此,您可以使用以下内容创建以下文件(例如,将其命名为 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 Project="$(MSBuildProjectDirectory)[RelativePathToFile]\WebApplication.Extension.targets" Condition=" '$(Configuration)' == 'Debug' "/>

编辑 2:

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

关于asp.net - Visual Studio "Add As Link"在调试时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13865534/

相关文章:

c# - 在 asp.net webform 中检测移动版本 - c#

c# - 如何为 HTML <video> 标签缓存 mp4 视频?

asp.net-mvc-3 - MVC-3 和 Html.Serialize(ASP.NET 4、MVC3)

c# - 获取MVC3中复选框的值

javascript - jQuery - 未捕获的类型错误 : Object [object Object] has no method 'typeahead'

visual-studio-2010 - 如何使用命令行构建 SharePoint 2010 包?

c# - 在 Visual Studio C# 中,有没有办法将 Web Config 转换应用于 Javascript 文件?

c# - 为什么通过 ASP.NET 方法运行查询比 native SQL 花费更长的时间?

ASP.NET 与 WCF 聊天

c++ - 用于 Visual Studio 2010 的 C++ 内存泄漏工具