Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio "Add As Link" not working while debugging

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.

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.

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.

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.

Thanks,

like image 660
Chattah Avatar asked Dec 13 '12 17:12

Chattah


People also ask

How do I add a link in Visual Studio?

1) Right-click “utilities” folder on Visual Studio and select “Add -> Existing item”. 2) Select the file to add, clicking on it ONCE (do not double-click). 3) Click in the arrow next to the “Add” button and select “Add As Link”.

How do I enable debugging in Visual Studio?

In the Visual Studio toolbar, make sure the configuration is set to Debug. To start debugging, select the profile name in the toolbar, such as <project profile name>, IIS Express, or <IIS profile name> in the toolbar, select Start Debugging from the Debug menu, or press F5.

How do I set debug path in Visual Studio?

In Solution Explorer, right-click the project and choose Properties. In the side pane, choose Build (or Compile in Visual Basic). In the Configuration list at the top, choose Debug or Release. Select the Advanced button (or the Advanced Compile Options button in Visual Basic).


2 Answers

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.

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> 

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" /> 

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.

Edit: 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' "/> 

Edit2:

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.

like image 52
Maxim Kornilov Avatar answered Sep 21 '22 16:09

Maxim Kornilov


A solution to automatically copy all linked content files to their "virtual" locations at each build using MSBuild is here:

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

This ensures that your linked files will be available to the browser when you hit F5.

like image 24
user1147862 Avatar answered Sep 23 '22 16:09

user1147862