Click here to Skip to main content
Click here to Skip to main content

Share your user control across multiple web applications

, 11 Sep 2003
Rate this:
Please Sign up or sign in to vote.
How to share a user control across multiple websites.


One of the major critics against the user controls is that everyone thinks that it is not possible to share them across multiple web applications and that you need to do some cut and paste to reuse them. Well, we will see in this article that this critic is wrong and that it is possible to share user controls across multiple applications. Lets see how and the steps to achieve this.

UserControls library

First let's create a web project in Visual Studio .NET that will contain all user controls that we need to share.

For our demo, we add a new user control named DateBox.ascx to the project and add a calendar and two labels to it.

UserControl's code behind

In order to demonstrate that it is also possible to reuse the code behind along with the user control we will add some custom code to the page_load event.

public string Company 
   get { return _company; }
   set { _company = value; } 

public string Year 
   get { return _year; }
   set { _year = value; } 
protected void Page_Load(object sender, System.EventArgs e)
   Response.Write("DateBox Page Load Event successfully called!");
   lblDate.Text = Year;
   lblCompany.Text = Company;

private string    _company,

Note : It is also possible to use the ascx files as a single file and write directly the custom code in it.

Sharing the code behind assembly of our usercontrols library

As we want to share our library across multiple web applications we must register the assembly that holds the code behind of our user control in the GAC (Global Assembly Cache). In order to register our assembly in the GAC we must follow several steps.

First, generate a strong name key for it and set the AssemblyKeyFile attribute in the assemblyInfo.cs of our project with the keys, filename and path. We can generate a key using the DOS sn.exe utility.

sn.exe -k ControlsLibraryKeys.snk

Next, we build our solution and to register our assembly in the GAC we choose to either drag and drop it directly in the \WINNT\assembly folder or use the following DOS command.

Gacutil.exe -i yourassemblypath

We must also inform the ASPX compiler that we want him to check for our new shared assembly. To do that we need to edit the machine.config. This file contains most .NET configuration of our local machine. You can find it in the following directory:


Load the file in a plain text editor and search for the <assemblies> element, add the following child element to it :

<add assembly="UserControlsLibrary, Version=, 
       Culture=neutral, PublicKeyToken=29f4c43ca2dde360"/>

These values might change depending on your current settings. You can get the current publicKeyToken of your assembly from the \WINNT\assembly folder.

Setting up our usercontrols library consumer

To use our library we will create another web project in Visual Studio that will consume our library. In order to use the assembly that contains the code behind code we must add a reference to it in our project.

We can access the assembly as we registered it in the GAC but how can we have access to the .ascx file from our aspx page ? IIS will answer to this question for us. IIS has a neat feature called virtual directories, virtual directories allow us to add to a website root a sub-directory that maps to a different physical path.

We will use this functionality to map a new virtual directory called "controls" to the path where our .ascx file reside. We can add a new virtual directory to the root of our web project using the Internet Service Manager.

Note : By default IIS associates an application to the virtual directory, as we don't need it we clicked the "remove" button to remove it.

Rise of the controls

Now that we have the full setup lets test our controls using the default webform1.aspx page.

In order to use any user control we must first add the register directive in the HTML part of our page.

<%@ Register TagPrefix="DNG" TagName="DateBox" 
           Src="~/Controls/DateBox.ascx" %>

And here is the trick, watch carefully the SRC. We are tricking the ASP.NET engine by using an app-based path with the ~ prefix instead of a classic virtual path. If we had used a virtual path ASP.NET would have thrown the following error "maps to another application, which is not allowed". Using and app-based path ASP.NET doesn't notice that the file comes from a different application and we can use our ascx files, great !

Lets move on and add our user controls to our page and compile the project.

<DNG:DateBox id="DateBox1" Company="DNG" Year="1984" runat="server" />

If we did everything correctly our shared user control should appear on our test page.

Looks nice Wink | ;)

Using the controls from the code behind

We can also use and set the user control properties directly from the code behind of our aspx page.

public class WebForm1 : System.Web.UI.Page
   protected DateBox DateBox1;
   private void Page_Load(object sender, System.EventArgs e)
      DateBox1.Company = "Amiga";
      DateBox1.Year = "1985";

Using the controls dynamically

For people who love using the dynamic features of ASP.NET you can also use this solution to add dynamically a shared user control from our library.

public class WebForm1 : System.Web.UI.Page
   private void Page_Load(object sender, System.EventArgs e)
      DateBox dynDateBox = (DateBox) 
      dynDateBox.Company = "Commodore";
      dynDateBox.Year = "1980";


In this article, we saw how to break the web applications barrier by using an app-based path and how to create and share a user control library across multiple web application. For demo purposes we only created one consumer (web application) but you can of course create as many consumers as you want, the only thing you need to do is to repeat the steps above for each new consumer.

This article was originally published in French at


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Thomas Zumbrunn
Web Developer
Switzerland Switzerland
Thomas is the founder of and the .NetPolls web control. He's holding an MCDBA / MCSD .NET in C# and can be reached through this url :

Comments and Discussions

Generalshared usercontrol references other dlls Pinmemberpriya.vjn15-Jul-09 10:29 
QuestionShare usercontrols across applications PinmemberPradosh Manerkar2-Jun-09 22:38 
GeneralI've tested this article, but come across some problems Pinmemberivanxin14-Jan-08 19:53 
General[Message Removed] Pinmemberstonber6-Oct-08 8:35 
GeneralServer Error in '/' Application. Pinmembermohd faiz18-Nov-06 3:00 
GeneralA newer solution to the problem [modified] PinmemberSkwirel Munkee26-Jul-06 2:23 
GeneralASP.Net 2.0 Pinmemberss_doug21-Oct-05 5:57 
GeneralRe: ASP.Net 2.0 PinmemberPChott24-Jan-06 22:31 
GeneralRe: ASP.Net 2.0 Pinmemberss_doug26-Jan-06 5:20 
GeneralRe: ASP.Net 2.0 Pinmemberenteng.kabisote5-Dec-06 23:29 
Questionhow can i set the property codebehind? Pinmemberhuangsj13-Sep-05 23:34 
QuestionDebugging? PinmemberEisenB23-Aug-05 14:08 
GeneralBuild WebSite In VS.Net Beta2 Pinsusssharaabi23-Aug-05 6:35 
GeneralRe: Improvement And Antother Imrpovement PinmemberDotNetAppDeveloper28-Jul-05 13:58 
GeneralRe: Improvement And Antother Imrpovement PinsussSteve Segarra4-Mar-05 4:59 
Generalhi sorry Pinmemberryanheaven16-Dec-04 5:15 
GeneralParser Error Message: allowDefinition='MachineToApplication' beyond application leve Pinmembermallik n8-Nov-04 14:46 
GeneralRe: Parser Error Message: allowDefinition='MachineToApplication' beyond application leve Pinmemberdnbaj25-Aug-06 2:51 
GeneralShare user control Pinmemberrimmabt6-Aug-04 8:02 
GeneralIt works Great PinmemberI Piscean7-Apr-04 7:32 
GeneralExcellent Article Pinmemberrhinohere19-Dec-03 7:05 
QuestionImprovement..? Pinmemberlluispi3-Oct-03 7:34 
AnswerRe: Improvement..? PinmemberCyinade28-Sep-04 4:35 
AnswerRe: Improvement..? Pinmemberpowiz22-Oct-04 0:55 
AnswerRe: Improvement..? Pinmembermanjulakrishna9-Feb-05 6:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 12 Sep 2003
Article Copyright 2003 by Thomas Zumbrunn
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid