Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 463
Jan Aagaard Avatar asked Mar 27 '09 07:03

Jan Aagaard


People also ask

Does Visual Studio have a web server?

Visual Studio lets you test with different web servers, including IIS Express, Internet Information Services (IIS), External Hosts, or Custom Web Servers. You can use any of these web servers with a file-based web application project.

When Visual Studio creates a web project which is the port used for the web server?

The publishUrl port is set to port 8172, which is the default for Web Deploy. The destinationAppUrl port is set to port 80, which is the default for IIS. If, in later steps, you are unable to connect to the remote host from Visual Studio using the host name, test the server's IP address in place of the host name.


2 Answers

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!

like image 64
womp Avatar answered Sep 25 '22 02:09

womp


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.
like image 31
Lennaert Avatar answered Sep 26 '22 02:09

Lennaert