Visual Studio "Add As Link" not working while debugging

It can also be solved by pasting the following snippet into the end of your .proj file which will contain the linked files

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


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.


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.