Create A Utility Class Containing Site-Wide Functions

2.91 (11 votes)

In Classic ASP, the typical model for managing site-wide functions is to create one or more server-side includes containing libraries of code. The OOP nature of ASP.NET offers a much more elegant solution.

I used to love include files in Classic ASP. I would group together similar functions and subroutines into collections of files, and selectively include them at the top of a page based on the page's requirement. ASP.NET doesn't support this approach, and one of the first questions I asked in a newsgroup relating to ASP.NET was how to manage this?

One very kind poster told me to create a "utility" class, and reference it from my code-behind. Being completely new to OOP, I had no idea how to approach this, so they even posted some sample code, based on what they were using in professional applications:

public sealed class Utils // sealed to ensure the utility class won't be inherited

private Utils() {} // Ensure you cannot instantiate an object of this type
    //Add static methods which means you can reference them
    //anywhere in the project simply by using Utils.
    //This one keeps the name of the connection string in just 2 places - 
    //once in the Web.Config, and once in the Utils class.

    public static string GetConnString()
        return WebConfigurationManager.ConnectionStrings["myConnString"].ConnectionString;

To use this method is simple. Whenever a connection object is created one of the arguments passed to the constructor is the connection string. This is done as follows:

string connectionString = Utils.GetConnString();

This simple snippet also made me realise the difference between static and instance methods. And the best thing? Intellisense loves to colour "Utils" light blue.

You might also like...

Date Posted:
Last Updated:
Posted by:
Total Views to date: 13637

1 Comment

- Patrick Oriyomi

Create A Utility Class Containing Site-Wide Functions.

Do you have VB version of this Utils class?

Recent Comments

Pam 30/08/2017 11:30
In response to Sending Email in Razor Pages
Mike, RazorPages sound like a nice choice for somebody still working in ASP classic who wants to to...

Robby Robson 15/08/2017 00:43
In response to Routing in Razor Pages
Mike: great stuff. Now that .Core Standard 2.0 is formally out, how soon will you rewrite your book...

Satyabrata Mohapatra 28/07/2017 08:59
In response to Sending Email in Razor Pages
Bit off topic, but congratulation sir for your MVP award. You deserve it !!!...

Satyabrata Mohapatra 23/07/2017 16:43
In response to Razor Pages - The Elevator Pitch
@Dale Severin You can continue to build apps using web pages....

Satyabrata Mohapatra 23/07/2017 16:40
In response to Sending Email in Razor Pages
Thanks for sharing...learned a lot...

Gfw 22/07/2017 11:53
In response to Sending Email in Razor Pages
Question... Does System.Net.Mail support SSL?...

Dale Severin 20/07/2017 03:38
In response to Razor Pages - The Elevator Pitch
I work with razor web pages extensively. I appreciate the rapid development it permits me to I am as...

Obinna Okafor 14/07/2017 01:19
In response to Routing in Razor Pages
Thank you, Mike. Good post....

Satyabrata Mohapatra 11/07/2017 16:02
In response to Routing in Razor Pages
Very powerful routing system!!...

Cyrus 05/07/2017 03:41
In response to Razor Pages - Getting Started With The Preview
How can I trim packages and services as much as possible to use just razor pages? I don’t want to to...