O'Reilly Book Excerpts: ASP.NET in a Nutshell
User Controls and Custom Server Controls, Part 4by Matthew MacDonald and G. Andrew Duthie
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:
- Use the sn.exe command-line utility to create a public key pair for use in signing your control:
- Add the
AssemblyKeyFileAttributeto 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:
- Recompile the control.
- 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:
sn.exe -k Blog.snk
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
PATHenvironment 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
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=0.0.0.0, 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.
The following sites provide additional information on the topics discussed in this chapter:
- http://www.aspfr iends.com/aspfriends/aspngcontrolscs.asp
- Home page for the AspFriends.com mailing list (for any questions relating to building custom server controls in C#).
- http://www.aspfr iends.com/aspfriends/aspngcontrolsvb.asp
- Home page for the AspFriends.com 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 AspFriends.com 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 http://www.prosetech.com/.
Back to: ASP.NET in a Nutshell