Sharing User Controls Between IIS Applications

Published Thursday, December 04, 2008 4:36 PM

I know a lot of you have developed User Controls over the years, and that many of you have large web sites that re-use these controls.  I appreciate the scope of the issues many of you face in maintaining very large sites and the issues you face when making seemingly small changes in them.

A training session I attended this week reminded me of a bugbear many of you face:  when a large web site's been divided into multiple applications in IIS, there isn't an obvious way to re-use User Controls across those applications.  The instructor was a little bit more adamant; he said there wasn't so much as a good way to do it. 

That isn't the same as saying there's NO way to do it, however, and in this post I want to talk about a way to make it happen.  Whether it's good is something I'll leave up to you. :)

Screenshot of the Date control

To make this easy to explain, let's say that you have a simple ASP.NET control, like the Date.ascx control shown here, which simply displays the date in a few different calendars.  Let's say that you want this on all of the pages in your site, and let's also say that due to reasons beyond your control your site is divided into a couple applications in IIS, which I'll call WebSite1 and WebSite2, organized side-by-side.

If you want the same ASCX file to be used by both IIS applications, you might falsely reach the conclusion that you're stuck.  After all, pages in one application can't use an ASCX file from another application.  However, you can do something extra-sneaky to make IIS treat a folder's contents as part of two different applications at the same time, permitting you to maintain only one physical copy of the ASCX file on the server.

To do this:

  1. Create a folder outside your site's web root (like, say, D:\UserControls) and copy the User Control and its code file there.
  2. Open Internet Information Services Manager (Inetman.exe) and find the root node for one of the applications that you want to share the control.
  3. Right-click the node, select "Add Virtual Directory...", and add the directory from step 1 as a virtual directory under the IIS application node.
    Screenshot of Step 3
  4. Repeat steps 2 and 3 for the other applications you want to be able to use the control.

When you're done, each application will have a virtual directory underneath it which contains the User Control's ASCX file and code file.  When ASP.NET compiles the two sites, the ASCX files will be compiled as a part of -- and be usable in -- both applications.

This solution, of course, isn't perfect.  In addition to the added setup cost up front, your applications will still separately compile and build the assembly for the user control, which is redundant effort.  If you care about saving compilation time, you won't gain any advantage from doing this.  However, if maintainability is your goal, the above does eliminate the need to maintain separate copies of your user controls on the server.

Let me know if you find some value from this and can simplify your site's management.

Clay Compton
ASP.NET QA Team

 

Filed under: ,

Comments

# Kalyan said on Friday, December 05, 2008 1:54 AM

Just a thought, can a junction point be created using linkd.exe and use it for the above scenario ?

# Darren Kopp said on Friday, December 05, 2008 2:12 AM

yeah, we do that with master pages too. all of our master pages are in virtual directories and just do ~/Skins in all our applications.

# Peter Bucher said on Friday, December 05, 2008 7:24 AM

Clay Compton vom ASP.NET Team zeigt in seinem Blog eine Möglichkeit , um UserControls mithilfe von virtuellen

Leave a Comment

(required) 
(required) 
(optional)
(required)