views:

569

answers:

2

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?

+1  A: 

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!

womp
+1  A: 

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.
Lennaert
Thanks a lot for this suggestion. I'm afraid I can only mark one response as the answer.
Jan Aagaard
No problem, such is life :)
Lennaert
How does this approach work with debugging? Can a put a break point in JavaScript file that is overwritten by the build?
Jan Aagaard
Actually, I haven't been able to get JavaScript debugging working at all in VS, so I can't tell you that. I think breakpoints will be removed while building.My approach now is to put an invalid character somewhere, the debugger will break on that. Mind you, I'm a bit ashamed of that solution :)
Lennaert
This is getting out of topic, but 1) make sure that script debugging is not disabled and that you get an notification for every error (Tools > Internet Options > Advanced) and 2) try launching IE using the Start Debugging in Visual Studio instead of attaching to iexplorer.exe.
Jan Aagaard
What is your procedure when you add a new control to the library? Do you manually add the files to the different ImportedUserControls using 'Add New Item'? Or are you using the Web Site model and just refresh? Or something else?
Jan Aagaard
After I add a UC to the WebControlLibrary, I only build the projects in which it is needed - that causes it to be copied (because of the copy-command above in the build event)
Lennaert