|
download source code
Introduction
One of the most frequent gripes most programmers have about ASP.NET 2.x and 3.x is that you can't implement include files across several web sites. Remember the good old days in Classic ASP when you could just put a reference to a virtual file in the code and it would automagically work? In ASP.NET, Microsoft recommends that you use DLLs to share code, but this can be hazardous and complex, especially when updating DLLs in a production environment. It would be much easier and intuitive if you could share source code files in ASP.NET. Some people like to gerry-rig the App_Code folder to do this, but that can cause more problems than it solves.
I recently wanted to share a piece of code that makes a database call with several other web sites. After Googling around for awhile, I didn't find anything that was exactly what I was looking for, so I got creative and came up with the solution described here. Just like in Classic ASP, this can be done easily using virtual directories. (To be fair, there is a Microsoft support page that describes something similar, but they make it way too complicated.)
Summary
To share a control across different web projects, you simply:
- Create a control in a new web site.
- Configure IIS properly to handle the control in ASP.NET.
- Create a virtual directory in the web site that wants to use the shared control.
- Register the control in any webpage that wants to use it and instantiate the control in the form.
- Use the control at will.
Detailed instructions are provided below.
Getting Started: Create a Shared Control
The first thing you need to do is create a new web site. But there's no need to create a solution or anything like that.
Open up Visual Studio and select New Web Site from the File menu. Create whichever type of web site you want.
Next, you need to configure IIS properly. This involves setting creating an application and setting it to ASP.NET 2.x or 3.x.
Then you need to create a simple ASCX file (user control) called GetInfo. To avoid compilation errors that can happen when you make changes in a production environment, I strongly recommend that you keep the code in a single file.
Here's the code for my simple user control. You pass it an account and a company and it returns a SQL statement that can be used by any web project on the webserver.
<%@ Control Language="VB" ClassName="GetInfo" %>
<script runat="server">
' CHANGING THIS CODE WILL CHANGE EVERY WEBPAGE THAT DISPLAYS ALLERGIES
Function GetAllergies(ByVal Account As String, ByVal Company As String)
GetAllergies = "exec sp_GetAllergiesNEW '" + Account + "','" + Company + "'"
End Function
</script>
Sharing the Control with an Existing ASP.NET Web Site
In order to use a shared control in another web site, you first need to configure a virtual directory using IIS.
- Open IIS and right click on the folder containing the desired website. (This is presumably a different website on the same webserver.)
- Select
New->Virtual Directory... and specify the name and location of the directory (e.g. SharedCodeVB in C:\Inetpub\wwwroot\SharedCodeVB).
- Modify the properties of the new virtual directory so it is an ASP.NET directory.
This includes creating an application and setting it to ASP.NET 2.x or 3.x, as explained earlier.
Using the Shared Control on a Web Page
-
To use the control in a specific web page, put a register statement similar to this at the top:
<%@ Register TagPrefix="uc1" TagName="SharedCodeVB" Src="SharedCodeVB/GetInfo.ascx" %>
NOTE: The Visual Studio compiler may complain that this control does not exist. Since these websites are not compiled, it doesn't matter.
-
Put an instance of the shared control somewhere in the <form>.
<form id="form1" runat="server">
<uc1:SharedCodeVB id="SharedCodeVB1" runat="server" />
-
Call the function in the shared control like this:
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim strSQL As String = SharedCodeVB1.GetAllergies("012345", "MAIN")
Label1.Text = strSQL
End Sub
Happy Programming!
Additional Reading
Attachments
About the Author
Scott D. Smith has a degree in operating systems at Chico State University (CSU).
He has been a database administrator and .NET applications programmer for 7+ years
at Prairie Cardiovascular Consultants. He is a Graduate Assistant for the Dale
Carnegie program based out of Quincy, Illinois. His Quality Treatment Plan web
pages have been demonstrated around the world by Dr. James T. Dove, M.D., president
of the American College of Cardiology (ACA). If you have questions or comments about
this article, email Scott at ssmith(at)prairieheart.com.
|