title: Add File as a Link on Visual Studio - Debug vs Publish
body: <p>Every time I have to link a file into an ASP.NET project as link, there is always something I forget to do, because Visual Studio is way too buggy and annoying with these.
  There is a bug/limitation with it so it does not copy the file into the Debug output folder, and we must change the properties of the file so that it is copied into the Publish output folder as well.</p>
  
  <p>I learned this in the hard way, after a lot of research, and I created an AfterBuild target to help in making this process easier. However, there is still some manual work to do when adding the file.</p>
  
  <p>Here is an example for a project I maintain. Say we want to add the file YIUCARD in the “utilities” folder of the ASP.NET project.</p>
  
  <p>1) Right-click “utilities” folder on Visual Studio and select “Add -> Existing item”.</p>
  
  <p><img src="https://i.stack.imgur.com/8SIGa.png" alt="Step 1"></p>
  
  <p>2) Select the file to add, clicking on it ONCE (do not double-click).</p>
  
  <p><img src="https://i.stack.imgur.com/i4WOZ.png" alt="Step 2"></p>
  
  <p>3) Click in the arrow next to the “Add” button and select “Add As Link”.</p>
  
  <p><img src="https://i.stack.imgur.com/EAI6s.png" alt="Step 3"></p>
  
  <p>At the moment, this will do nothing. The file won’t be copied to any folder (Debug and Publish). It is just added to the project. </p>
  
  <p>4) Right-click in the file and open Properties.</p>
  
  <p><img src="https://i.stack.imgur.com/a3h65.png" alt="Step 4"></p>
  
  <p>5) Change:
  “Build Action” to “Content”
  “Copy to Output Directory” to “Copy always”</p>
  
  <p><img src="https://i.stack.imgur.com/GOCs0.png" alt="Step 5"></p>
  
  <p>At this time, the file will be copied into the Publish output folder (anywhere we define when publishing).
  However, when we debug locally (F5), it won’t work because the file is not copied into the local “utilities” folder on the codebase.</p>
  
  <p>6) Save the changes on Visual Studio, by selecting “Save All”. This is so that the “.csproj” file is saved, as we made changes on it and we will now edit it manually.</p>
  
  <p><img src="https://i.stack.imgur.com/2Vjj1.png" alt="Step 6"></p>
  
  <p>7) Open the “.csproj” in Notepad++.</p>
  
  <p><img src="https://i.stack.imgur.com/G7VIR.png" alt="Step 7"></p>
  
  <p>8) Add the file to the "BeforeBuild" event (here's mine):</p>
  
  <p><img src="https://i.stack.imgur.com/gbZBI.png" alt="Step 8"></p>
  
  <pre><code> &lt;Target Name="BeforeBuild"&gt;
      &lt;ItemGroup&gt;
        &lt;UtilitiesFiles Include="..\path\to\file\REDOPXX.NEW" /&gt;
        &lt;UtilitiesFiles Include="..\path\to\file\REDFMTST" /&gt;
        &lt;UtilitiesFiles Include="..\path\to\file\JOBCARD" /&gt;
        &lt;UtilitiesFiles Include="..\path\to\file\YIUCARD" /&gt;
      &lt;/ItemGroup&gt;
      &lt;Copy SourceFiles="@(UtilitiesFiles)" DestinationFolder=".\utilities" SkipUnchangedFiles="true" /&gt;
    &lt;/Target&gt;
  </code></pre>
  
  <p>9) I have an "AfterBuild" event for Release mode that automatically publishes the project for me, so that the files go directly to the output folder I want:</p>
  
  <pre><code>  &lt;Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release' "&gt;
      &lt;PropertyGroup&gt;
        &lt;OutputDir&gt;..\path\to\publish\MyProject&lt;/OutputDir&gt;
      &lt;/PropertyGroup&gt;
      &lt;Message Importance="High" Text="Output DIR: $(OutputDir)" /&gt;
      &lt;RemoveDir Directories="$(OutputDir)" ContinueOnError="true" /&gt;
      &lt;MSBuild Projects="MyProject.csproj" Properties="Configuration=$(Configuration);WebProjectOutputDir=$(OutputDir);OutDir=$(OutputDir)bin\" Targets="ResolveReferences;_CopyWebApplication" /&gt;
    &lt;/Target&gt;
  </code></pre>
  
  <p>10) Save the file in Notepad++ and reload it on Visual Studio.</p>
  
  <p><img src="https://i.stack.imgur.com/KjWdO.png" alt="Step 10"></p>
  
  <p>When you build the project, the file will be copied to both Debug and Release/Publish folders!</p>
  
  <p><strong>However, I would like to know if there is any easier/intuitive way to do this.</strong></p>
  
  <p>You may say that this would be fixed by changing the Debug output folder to the same as the Release/Publish folder, so that the files would be there after the first time I published.
  However, please note that there is a bug with this, as well. If I use an output folder other than the "bin\", aspx files will complain that they can't find the assemblies. Even if I set "Copy to Output Directory" to "Copy Always", it will complain that "Global.asax.cs" could not be found.</p>
  
  <p>Please see these related questions:</p>
  
  <p><a href="https://stackoverflow.com/questions/1598829/parser-error-message-could-not-load-type-testmvcapplication-mvcapplication">Parser Error Message: Could not load type &#39;TestMvcApplication.MvcApplication&#39;</a></p>
  
  <p><a href="https://stackoverflow.com/questions/2005747/could-not-load-type-namespace-global-causing-me-grief">&quot;Could not load type [Namespace].Global&quot; causing me grief</a></p>
  
  <p><strong>Is this fixed/improved in the newer versions of Visual Studio?</strong></p>