How do I share common web resources between web applications in Visual Studio?

0 votes
asked Mar 27, 2009 by jan-aagaard

I have three web application projects that share a common library of custom server controls. Sharing the code behind files is easy - they are compiled into a dll that i reference in the other projects using a project reference. But how do I handle files like JavaScript, style sheets and images? I am trying to do this "the Visual Studio way" so that it is as easy as possible to understand and debug.

The current setup is somewhat like this:

CommonControlsWebApp
+- CustomControls
+- resources
   +- images
   +- scripts
   +- stylesheets

WebApp1
+- resources*

WebApp2
+- resources*

WebApp3
+- resources*

*) Virtual directory in IIS.

The virtual directory on each web app is pointing to the resources directory in my CommonControlsWebApp. This is configured in IIS. Visual Studio is not able to understand this link, so debugging requires me to attach to the IIS-process manually.

Another solution could be to include every resource in the common dll using WebResourceAttribute. But that would turn src links into something like WebResource.axd?d=GUID when I view the source of my web pages, and I'm afraid that this will make it pretty confusing to debug.

A third solution would be to use Subversion to include the same files in all three solutions. Nobody in the team has tried this before, so we are not sure about how well this would work.

I feel that I am missing some great, obvious solution on how to setup the projects. Any suggestions?

3 Answers

0 votes
answered Mar 27, 2009 by womp

Many control developers bundle their webresources using the second method you describe - adding them as embedded resources in the .dll. This way, you can distribute the controls without having to copy a bunch of folders and files into every website that wants to use them, and make sure the folder paths all match, etc. You also gain the advantage of much easier localization/internationalization of your resources.

However, the WebResource.axd thing is a drawback. You can usually "debug" the URL's with something like Firebug to see what actually got returned from the request, but I agree, it can be a pain if something isn't working correctly. Another drawback is if you have dozens of resources for one server control, especially if they are referenced multiple times each (think of a treeview with hundreds of small images on the nodes) - then the URL's can add a ton of actual bytes to the final HTML output.

If your distribution/localization needs outweigh your optimization/debugging needs, I would consider embedding the resources.

If not, then you could definitely do #3 - copying everything into your website using Subversion. Check out SVN Externals for how to set it up. A little time invested into learning it now might save you tons down the road!

0 votes
answered Mar 27, 2009 by lennaert

I took the easy way out and put all my projects in a single solution. Then I created a WebUserControlLibrary project in which I put all the shared controls.

To get them in the other project, I then configured the build event for each project to copy the UCs into a special folder:

copy "$(SolutionDir)WebUserControlLibrary\UserControls\*.ascx"
   "$(ProjectDir)ImportedUserControls\UserControls"

And of course reference the WebUserControlLibrary from the projects that need it.

Advantages of this approach:

  • Debugging in Visual studio works,
  • No code duplication

Disadvantages:

  • Clunky solution :)
  • After changing an .ascx-file, the project has to be rebuilt. This means it's not possible to change the html and refresh the page in IE and see the results directly.
0 votes
answered Sep 15, 2017 by nugs

I have a couple of options to throw into the bucket:

Option 1: Use a Shared Project for your resources: http://rion.io/2017/03/22/sharing-is-caring-using-shared-projects-in-asp-net/

Options 2 Convert your Class Library into a NuGet package. I don't believe you have to do much when packing other than the following to include all folders and files withing the NuGet project folder:

c:\nuget.exe pack YOURPROJ.csproj -IncludeReferencedProjects -Prop Platform=AnyCPU -Exclude **.tt -Exclude **.txt

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...