Accessing Common Code, Constants, and Functions in an ASP.NET ProjectBy Scott Mitchell
When moving from classic ASP development to ASP.NET, one thing many developers find themselves needing to do is port their existing reusable code base from classic ASP to ASP.NET. With classic ASP, many developers created include files, which are files that would contain common functions, code, and constants. These include files could then be used in any classic ASP page by simply adding an
#includedirective at the top of the page:
<!--#include file="includeFile.asp"-->. Include files in classic ASP were useful for centralizing code so that common functions and constants didn't need to be repeated in numerous pages. (For more information on using
#includein classic ASP, be sure to read: The Low-Down on
#include is still supported to a degree, but there are better ways to create a common
repository of functions, code, and constants. In this article we'll first examine the optimal approach for
storing Web application-wide constants; next, we'll look at how to provide a means for creating functions common
to all ASP.NET Web pages in the Web application.
There are many times when various Web pages in a Web application need access to a similar set of constant values. The most common scenario is when the Web pages access database data. These pages, if accessing the database directly, need to know the connection string to the database. One approach is to simply hard-code the database connection string in each data-accessing page, but this approach can lead to quite the headache if the connection string changes, since it requires manually editing all pages where the constant has been duplicated.
A better approach is to use a centralized repository for storing such constants. This approach allows for the constant's value to be spelled out in precisely one location. The Web pages that need to access the constant's value can do so through a bit of code. With this technique, needed changes to the constant require only a change in one place, and the pages that use the constant are automatically updated accordingly.
The question, then, is where should these constants be stored, and how can they be accessed? The ASP.NET
file provides as good a place as any for storing simply scalar constants. The
Web.config file can have an
appSettings element added, in which you can provide a variable number of constant names and values.
For example, a database connection string could be defined in the
Web.config file like so:
Then, from an ASP.NET Web page, a constant value can be retrieved with just one line of code,
ConfigurationSettings.AppSettings("constantName"), like in:
Note: you will need to import the
System.Configuration namespace into your project for the
above code examples to work...
Web.config file you can even add your own constants "group," akin to the
For more information on this technique, as well as a more in-depth discussion on using
store application-wide constants, be sure to read: Specifying Configuration Settings in
Rather than using the
Web.config technique, you can roll your own constants managing class, as
Rachael Schoenbaum showed in her article Using XML to Share Constants Across
Creating Functions Common to All ASP.NET Pages in a Web Project
Many classic ASP developers have built up a rich set of functions that are used in a variety of their pages through
#includefiles. Imagine that we had the following classic ASP function, defined in a
#includefile and used in a number of classic ASP pages:
This VBScript function takes in a Recordset and displays the name, social security number, and department of the
employee in a pre-formatted way. Such a function might be used in numerous pages when working with employees in
order to display their vital information in a consistent format. Therefore, when migrating from classic ASP to ASP.NET,
the classic ASP developer might want to create a central function repository in his ASP.NET Web application that would
contain functions like
To provide such functionality in an ASP.NET project, create a class in your ASP.NET Web application project and
for each common function, create a
Shared function in the class. (Shared functions, or "static functions" in C#
terminology, are class methods that can be invoked without an instance of the class being first created. That is,
if you have a class named
ConstClass, and a method named
DisplayEmployee(), you can invoke
the method with:
ConstClass.DisplayEmployee(), as opposed to have to create an instance of the
To create a new class using Visual Studio .NET, right click on the Project name in the solution explorer and choose to
Add Class, as shown in the screenshot below. You will be asked to pick a name for the class, I would suggest using
something descriptive like
This will create a blank class file with code like:
Now, just add the function(s) you want to have accessible from all ASP.NET Web pages in this project. When adding the
function (or sub), be sure to use the keyword
Shared in the function's definition, as shown below.
For example, we could port the VBScript
DisplayEmployee() function, adding it to the class:
Now, in an ASP.NET Web page this method can be called like so:
The following example illustrates this common function in use:
When moving from classic ASP to ASP.NET there is often a need to port over common functions and constants. If you are new to ASP.NET you might not be familiar with the options for providing common code to all ASP.NET Web pages in a given project. This article examined methods for sharing both constants and functions across ASP.NET Web pages in a Web application.