Visual Studio 2015 - Shared Projects Reference Tab Missing on Web Project
Asked Answered
W

1

23

I upgraded to Visual Studio 2015 from our MSDN subscription because I was pretty excited to read about Shared Projects... No more managing 21382 nuget packages in our dependencies when all we want to do is reuse code.

So I built a test shared project with some code in it. Then I add a new empty web application on .Net 4.6 to the project. Now I expected to go to references and see a "Shared Projects" tab on the references window, but I do not see one.

Now I can add a class library to the same solution and I see the tab and can add the shared reference. However I cannot add the shared project to my web application.

I was rather hoping I could use this concept to share views with multiple MVC projects.

Am I missing something here, or are shared projects not compatible with Web Projects?

The only way I see around this is to have two projects for every web application I build. 1 for the code, and 1 for the content.

For example

XYZ.SomeWebSite.Code (Class Library Project Type) -> references Shared Project
XYZ.SomeWebSite (Web Project Type)

However going this route, I would not be able to push views, text files, css files, javascript files, etc into the web application.

Watteau answered 3/8, 2015 at 19:43 Comment(7)
I submitted a frown on this issue with details... Hopefully this is an oversite... It's starting to appear like Shared Projects and Protable Class Libraries were created specifically for Windows Store Apps and Windows Phone 8 mindsets...Watteau
Just found an RTM Release notes: visualstudio.com/en-us/news/vs2015-vs.aspx Web Project type is not listed as supported, but it's from the RTM, not finding anything on the release.Watteau
I noticed the exact same thing last week. Kind of annoying. We've got three web portal applications and would like to centralize our assets (css, images, themes, fonts). Shared projects seemed like a great way to solve the problem but when I tried to add reference saw the same thing you did. <sad trombone>Trismus
I recently came accross a problem in a production environment where I think using shared projects is bad for IIS Websites, and maybe that's why it's not there. I was told to spin up 2 sites in one app pool, and it blew up on me because my code was compiled into both sites via shared projects. They could not both load in the same app pool because all the types would exist twice... I was able to convince them to run 2 separate app pools, but it was still a light bulb moment as to why it could be bad.Watteau
As I had said previously, in our case we don't want to use them to share code, we want to use them to share assets (images, CSS, JavaScript, etc). I understand the situation you're talking about (although why anyone would dictate that both sites needed to run in the same app pool baffles me a bit) but to "throw the baby out with the bathwater" because under certain circumstances it doesn't work seems short-sighted.Trismus
Sorry, I wasn't really directing that last comment at you, I was just documenting my finding for other's reference as this is still a new topic that hasn't gotten a lot of exposure yet.Watteau
Related feature request - developercommunity.visualstudio.com/content/problem/12592/…Attenuator
W
44

[Temporary Answer]

I was able to work around the problem by manually editing the csproj file for my web application.

Here are the steps:

  1. Unload the web application project
  2. Edit the *.csproj file by right clicking the project and clicking edit...
  3. Now look for the ProjectTypeGuids element.

    {349C5851-65DF-11DA-9384-00065B846F21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}

The first guid is the guid for an ASP.Net MVC 5 Project, and the 2nd guid is for C#.

All you need to do is temporarily remove the first guid and the semicolon, leaving just the c# guid (the 2nd one).

Reload the project and add your shared projects. Once they are all added, edit the project file and put the first guid and the semicolon back.

Reload the project.

The Shared Project reference will still be there, and it will build and link into your web application.

Optionally, you can just manually add the shared project referrence, the format is like this

<Import Project="..\XYZ.UPlugin\XYZ.Plugin.projitems" Label="Shared" />
Watteau answered 3/8, 2015 at 21:1 Comment(9)
Update: Upon testing, if you create an Assets folder in a shared project it will not exist in the target projects physical disk space. However if you publish your site it will exist in the publish target. So if you want to test locally without publishing you'll need to map a virtual directory in iis to the assets folder in your shared project.Watteau
A decent pattern I've been using is to change the root namespace of your shared project to just it's root. For example, use "XYZ" instead of "XYZ.Something" then create a folder in it called Something. Put your code there and it will default to a namespace of "XYZ.Something" and keeps your code separate from your root level assets, asset/css/js folders, etc.Watteau
Update: If all you want to do is share code with web apps. I've changed processes by detatching all the code from the web app. I am no longer using Visual Studio Web Project types. Instead I just create a folder for a site and add it to the project as an existing site. Then I move all the code to it's own class library and load that dll in the web.config of the existing site, view engines, etc. Class Libraries can reference shared projects just fine. It is tedious to create a web app without the template though. So I make one from the template, and copy out it's web.config, then deleteWatteau
Taking your advice, I simply editing my csproj file and added your <import /> statement. No need to unload the project. When I clicked back into Visual Studio it detected a change and wanted to reload the project. After that the reference was there.Lett
Yeah, you can link in shared projects outside of the solution folder to, just be weary of doing so because it doesn't play well with source control's with working directories, like subversion specifically.Watteau
This advise will turn the web.project into Class Library. I dont believe it is the proper way of doing it.Cleanse
It turns it into a class library, you add your reference, then put the guid back and it goes back to whatever type it was before. There is no proper way to do this, it's not supported. That's why this answer says it's Temporary above, until there is a proper way to do it.Watteau
Worth noting that although it says <Import Project=... /> the extension is not the project file extension .shproj but the file .projitems. The latter file is in the same directory as the project file.Lilli
Also worth noting: manually adding the project reference also works for Visual Studio 2017 version 15.6.3 and also the new csproj file format (<Project Sdk="Microsoft.NET.Sdk.Web">). To create a shared project in VS 2017 ver 15.6.3 use the template named "Shared Project" at the root of the templates tree.Lilli

© 2022 - 2024 — McMap. All rights reserved.