Visual Studio“添加為連結”在調試時不起作用
我使用 Visual Studio 2010 維護大約 40 個不同的 Web 應用程序,這些應用程序在部署時都位於同一個網站上。這些項目在同一個解決方案中,但位於不同的項目中,因為它們各自做的事情非常不同。
我正在嘗試通過“添加為連結”選項在各個項目之間共享 css/js 文件,這樣我可以一次更新 css 或 js 文件並自動將更新反映在各個項目中,而無需建構和重新部署每個項目。
我遇到的問題是,當我試圖在我的 PC 上本地執行一個項目以進行調試時,這些連結的文件不起作用。我得到一個未找到的文件。
***我的想法:***我認為這是因為在本地執行應用程序時文件夾結構不同。我很好奇是否有一種方法可以僅在以調試模式建構時將文件複製到項目並相應地調整相對 URL,以便我能夠在發佈到我們的 Web 伺服器之前正確測試應用程序。
謝謝,
此問題的解決方案是複制在每次建構期間作為連結添加的內容文件(如 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 目標,它將在建構期間作為連結添加到項目文件夾的所有內容文件複製。