Tooling: "Add as Link" missing from Visual Studio

Created on 15 Aug 2015  路  29Comments  路  Source: aspnet/Tooling

Classic project types (eg. ASP.NET 4, Console Application, Class Library) have an "Add as Link" option in the "Add Existing Item" dialog. This allows one file to be shared across multiple projects, by adding the file to the project without physically copying the file to the project directory. This button is missing from the Add Existing Item dialog for ASP.NET 5 projects.

It appears you can achieve something similar through the shared section in project.json. However, files added this way do not appear under the project in Solution Explorer.

TFS Tracked

Most helpful comment

Based on @JimmyBoh 's idea, I added below lines to .xproj,
(for me, it was to add a shared AssemblyInfo.cs file to project properties)

<ItemGroup>
    <Compile Include="..\..\AssemblyInfo.cs">
      <Link>Properties\AssemblyInfo.cs</Link>
    </Compile>
</ItemGroup>

It worked and I could also see linked file in SolutionExplorer.

All 29 comments

@Daniel15 thanks for the note, we have it on our backlog but have not started on it yet. I'll reply here when we do.

Some scenarios are covered with the solution stated in https://github.com/aspnet/dnx/issues/550 (using "shared files" in project.json (https://github.com/aspnet/Home/wiki/Project.json-file#shared-files)

But when you need to have the same file on several projects, that it's not a compilable one, that does not work.

A common usage would be to have a shared config.json file shared between several projects on the same solution. 驴Is there any way to achieve that without linked files?

@davidfowl - Do files added like that appear in Solution Explorer? Can I add them via the "Add as Link" option in the UI? Not seeing the files in the solution explorer is a pretty big issue - How am I meant to know exactly which files are being included via the compile attribute? Manually looking in Windows Explorer isn't really an appropriate solution for something an IDE should handle :smile:

We don't show those files today but we are planning on adding support to show them.

Any ETA on this? I get that I can do it today but it's kind of black magic without seeing this file in solution explorer.

What if I want to share non cs file resources? While in active development, it is simple to add existing common js and less files as links and then have any modification within a project update the original source. The only alternative is to use a client-side package manager like bower, but while in active development this is too time consuming.
Please let us keep the csproj add files as links within visual studio.

Is the shared files approach still supported? Links were removed or moved to a different place, getting 404s.

Would be nice to have the ability to add files as links tho.

+1

I just needed this feature to share some Angular2 components with another application and couldn't use it.

Any ETA on this?

Thanks!

If you only need it for a file or two, a simple work-around is to edit your xproj with the same XML generated within a csproj. For example:

<ItemGroup>
    <Content Include="..\..\Some\Common\Project\file-to-be-shared.json">
      <Link>linked-copy.json</Link>
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content> 
</ItemGroup>

The only downside is that it does not work when using the dotnet CLI tools, just Visual Studio.

Is there a simpler way to workaround this issue other than manually adding each one to the xproj? This isn't exactly the best workaround for multiple files.

@tom-corwin It's hacky, I know. I'd create a powershell script or two depending on your use case.

If it's the same file (or few files) that must get copied to many projects then just make a simple CLI script to batch update all project files.

The second script would just open a file picker and allow the user to select a file, then update the xproj accordingly (meant to be used during active development).

I will try to throw one together later, but if you do make sure to post it here for everyone!

@JimmyBoh I will try to create one, but it will probably be a csproj since I'm not familiar with PowerScript at all :stuck_out_tongue_winking_eye:

@davidfowl The links that you provided isn't working. Can you please provide the updated link?
Thanks.

Would appreciate the tooling allowing for adding files as links sooner rather than later. I am having to manually add in linked files by editing the xproj which isn't very productive.

Any ETA on this one? Its open for more than a year with two updates to Visual Studio 2015 in the mean time. I am trying to share same source across .Net Core (1.0) and classic 4.5, and this is a dead end.

I am trying to share same source across .Net Core (1.0) and classic 4.5,

@mvadu - I've found the least error-prone way of doing that is to move the code into the .NET Core project, and "Add as Link" from the classic 4.5 project. This will make it appear in both projects in the solution explorer.

If you need any conditional bits of code, you can use preprocessor directives.

Just adding support for the "Add as link" in Visual Studio. Huge efficiency gains to be created in agile development and multi-refactoring where shared source can be tested across development instances.

Is there a specific MS developer VS feature statistical report on how many developers rank which feature requests at what importance level. GitHub would be a good place to show this metric providing some real time visibility to what is important across the tooling environment.

Support for this will becoming in the next release when we move to .csproj.

+1 for needing this functionality. Makes a huge difference in ensuring no code duplication is necessary.

Chiming in to add my vote for this feature.

Linked files are supported in the latest version of VS2017 RC. @riiight have you tried with VS2017 RC and had issues?

vs2015 update 3 ..
Hold ALT key and Drag&Drop your file. (you ll see little shortcut icon on the mouse)

This will be fixed with the new csproj tooling in VS 2017

Is there a way to add multiple directories/files as links at once?
@705am _Hold ALT key and Drag&Drop_ doesn't work in VS2017. The dragged files get copied instead of linked.

Based on @JimmyBoh 's idea, I added below lines to .xproj,
(for me, it was to add a shared AssemblyInfo.cs file to project properties)

<ItemGroup>
    <Compile Include="..\..\AssemblyInfo.cs">
      <Link>Properties\AssemblyInfo.cs</Link>
    </Compile>
</ItemGroup>

It worked and I could also see linked file in SolutionExplorer.

I also wanted to know this. Thanks, this issue helped me

Was this page helpful?
0 / 5 - 0 ratings