One of the web applications I maintain still runs on ASP.Net 3.5. A specific feature required that there also be a .Net 4 version of the application, so I thought about how to share the source code for both .Net versions.
Essentially, this can be achieved by creating a new solution and new projects and having the projects link to the original source files. That’s ok so far and it works fine.
However, when it comes to the ASP.Net application itself, things do not work straight forward:
My build process invokes both aspnet_compiler and aspnet_merge where aspnet_compiler requires all markup files to be present in the project directory, as it seems to ignore the contents of the .csproj file.
So how do I get the markup files into the linking project without manually copying the files? The process should be performed automatically, so that any changes in the original source are propagated to the linking project.
Luckily I found this article Using Linked Files in ASP.Net Applications, which unfortunately has been removed recently. In principle, you need to specify new build targets that invoke the file copy for the linked files. While the original solution only covered copying “content” files, I extended it to also copy “compile” files:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Target Name="CopyLinkedContentFiles"> <Copy Condition=" '%(Content.Link)' != '' " SourceFiles="%(Content.Identity)" DestinationFiles="$(OutputPath)\..\%(Content.Link)" SkipUnchangedFiles="true" /> <Message Text="Copying linked file %(Content.Link)" /> </Target> <Target Name="CopyLinkedCompileFiles"> <Copy Condition=" '%(Compile.Link)' != '' " SourceFiles="%(Compile.Identity)" DestinationFiles="$(OutputPath)\..\%(Compile.Link)" SkipUnchangedFiles="true" /> <Message Text="Copying linked file %(Compile.Link)" /> </Target> <PropertyGroup> <BuildDependsOn> CopyLinkedContentFiles; CopyLinkedCompileFiles; $(BuildDependsOn); </BuildDependsOn> </PropertyGroup>
Save the file as CopyLinks.targets in your project directory. Then edit the .csproj file to add the line
<Import Project="$(MSBuildProjectDirectory)\CopyLinks.targets" />
(find other <Import> sections and insert there).
As you build your project, the linked files will now be copied to the respective location in the project linked the original files.
The complete targets file can be downloaded here.