oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button O'Reilly Book Excerpts: ASP.NET in a Nutshell

User Controls and Custom Server Controls, Part 4

by Matthew MacDonald and G. Andrew Duthie

Related Reading

ASP.NET in a Nutshell
By G. Andrew Duthie, Matthew MacDonald

In this final installment in a series of excerpts from ASP.NET in a Nutshell, learn how to share ASP.NET controls across applications.

Sharing Controls Across Applications

The architecture of the .NET Framework makes using a custom server control or other assembly as simple as copying that assembly to the bin subdirectory of your application and adding the appropriate directives and tags to your page. However, there may be times when you would like multiple applications on the same machine to be able to use the same control, without having multiple local copies of the control's assembly floating around.

Fortunately, .NET addresses this need with the Global Assembly Cache (GAC), a repository of shared assemblies that are accessible to all .NET applications on a given machine. Adding your own control assemblies to the GAC is a relatively straightforward process that requires four steps:

  1. Use the sn.exe command-line utility to create a public key pair for use in signing your control:
  2. sn.exe -k Blog.snk
  3. Add the AssemblyKeyFileAttribute to the file containing the control code, passing the path to the keyfile created in Step 1 as an argument. (This is an assembly-level attribute, so it should be placed outside of any namespace or class definitions.) When compiled, this attribute will result in a strongly named assembly that can be placed in the GAC:
  4. [assembly: AssemblyKeyFileAttribute("Blog.snk")]
  5. Recompile the control.
  6. Add the control to the GAC, either by dragging and dropping the assembly in Windows Explorer or by using the gacutil.exe utility, as follows:
  7. gacutil -i Blog.dll

TIP:   Note that as with the csc.exe and vbc.exe command-line compilers, using the sn.exe and gacutil.exe utilities without a fully qualified path requires that you have the path to these utilities registered as part of your PATH environment variable. The sn.exe and gacutil.exe utilities are typically located in the \FrameworkSDK\bin directory, which is installed either under ProgramFiles ->Microsoft.NET or ProgramFiles ->MicrosoftVisualStudio.NET, depending on whether you've installed just the .NET Framework SDK or Visual Studio .NET.

In This Series

User Controls and Custom Server Controls, Part 3
Part 3 of this four-part excerpt from ASP .NET in a Nutshell covers custom server contols.

User Controls and Custom Server Controls, Part 2
In Part 2 in this series of excerpts from ASP.NET in a Nutshell, learn ASP.NET custom server controls.

User Controls and Custom Server Controls, Part 1
In part one from this series of book excerpts from ASP.NET in a Nutshell, get an overview on ASP.NET controls, and learn about ASP.NET user controls.

Once you've added the control assembly to the GAC, you can now use it from any application on the machine. One caveat: to use custom controls that are installed in the GAC, you must supply the version, culture, and public key information for the assembly when adding the @ Register directive for the control, as shown in the following snippet (which should appear on a single line):

<%@ Register TagPrefix="aspnetian"
Namespace="aspnetian" Assembly="Blog,
   Version=, Culture=neutral,
PublicKeyToken=6bd31f35fc9a113b" %>

If you've added your control to the Visual Studio .NET toolbox, when you use the control from the toolbox, the correct @ Register directive will be generated for you automatically.

Additional Resources

The following sites provide additional information on the topics discussed in this chapter:

Home page for the mailing list (for any questions relating to building custom server controls in C#).
Home page for the mailing list (for any questions relating to building custom server controls in Visual Basic .NET).
http://www.aspfriends .com/aspfriends/aspngreuse.asp
Home page for the mailing list (for general reuse questions). Whether you're working on user controls or custom server controls, you can get good answers to your questions on this list.
The DotNetJunkies site, run by Microsoft MVP Award winners Donny Mack and Doug Seven, contains many ASP.NET tutorials, including some on building custom server controls and user controls.

Matthew MacDonald is a developer, author, and educator in all things Visual Basic and .NET. He's worked with Visual Basic and ASP since their initial versions, and written over a dozen books on the subject, including The Book of VB .NET (No Starch Press) and Visual Basic 2005: A Developer's Notebook (O'Reilly). His web site is

G. Andrew Duthie is the founder and principal of Graymad Enterprises, Inc., which provides training and consulting in Microsoft Web development technologies.

Back to: ASP.NET in a Nutshell