Click here to Skip to main content
15,881,585 members
Articles / Web Development / ASP.NET

Base Page Class Website Development

Rate me:
Please Sign up or sign in to vote.
3.80/5 (9 votes)
29 Mar 2006CPOL3 min read 61.3K   34   14
This article describes a basic base page class to be used when developing .NET websites.

Introduction

This article deals with a basic BasePage class to use as the starting point to a "Base Page" for .NET 2.0+ web sites. As a side note, this base page class can be retrofitted to .NET 1.0+ with minimal work, if needed. I initially created it using Visual Studio 2005, and thus left the code as is.

Base Page Class

Now onto the specifics of being basic. I built this BasePage as a starting point for any website that I begin to develop. Everyone will have their own philosophical differences about what should be in this page class, but I chose only that which truly fit the functionality I need when starting a project. As the needs arise, I may add more security handling functionality, or region specific code.

The Base Page class has two purposes in life:

  1. Handle unexpected errors that have occurred during a page request.
  2. Wrapper functions for getting request parameter values, and dropping browser cookies.

Unexpected Errors

After development is complete, and the users begin to flock to your website, there is a slight chance they will encounter an unexpected error (or exception). When that one user (or many users) encounters an error, you may have lost that person forever, or even worse, they may tell all their friends about how this website just doesn't work. Using the BasePage class, you can have a common error handling routine to log this exception (stack trace and anything else needed), and/or notify yourself about the error via e-mail.

Some common practices I use in my global error handling:

  1. Log the ASPX page (and date-time).
  2. Log the exception message.
  3. Log the stack trace.
  4. Log the request object (querystring, form, cookie, session: values). By storing request object information, I get an idea of what the user was doing when the page crashed without having to communicate with the user to find out what happened.
  5. If using Forms (or Integrated Windows) Authentication, store the user's name, so I can notify him/her that I am diligently working on a solution. And then, notify him/her when the solution has been found and put into production.
  6. If possible, log items 1-4 (or 5) to a central bug repository so that it can be tracked, and status'd.

You may do more or less for logging exceptions. If you do more, please let me know what you do in addition to that listed above so that I may improve my error handling.

Wrapper Functions for URL Parameter Values

The Base Page class contains several handy wrapper functions for getting information out of the request object. For instance, a request has been made to the following page:

mypage.aspx?op=add&left_op=1&right_op=2

To get the integer values for "left_op" and "right_op", we can code the following:

C#
int left_op = Convert.ToInt32(Request.QueryString["left_op"];
int right_op = Convert.ToInt32(Request.QueryString["right_op"];

This can be error prone if a malicious user removes the "right_op" value from the querystring. By using the wrapper functions to get the querystring value, we have simplified our code, as well as provide some default values for the missing querystring parameters:

C#
int left_op = GetParmInt("left_op", 0);
int right_op = GetParmInt("right_op", 0);

At this point, everyone will have their own opinion on how they should handle missing or malicious querystring parameter values. While this demonstration is not a best fit for every project, the wrapper functions for getting information handle 90%+ of my needs.

The "Get Parameter" wrapper functions check the entire Request parameter collection, searching for the given parameter name with a given precedence:

C#
protected string GetParm(string parm, string defaultvalue)
{
  if (Request.Form[parm] != null && Request.Form[parm].Length > 0)
  {
   return Request.Form[parm];
  }

  if (Request.QueryString[parm] != null && Request.QueryString[parm].Length > 0)
  {
   return Request.QueryString[parm];
  }

  if (Session[parm] != null && Convert.ToString(Session[parm]).Length > 0)
  {
   return Convert.ToString(Session[parm]);
  }

  if (Request.Cookies[parm] != null && Request.Cookies[parm].Value.Length > 0)
  {
   return Request.Cookies[parm].Value;
  }

  return defaultvalue;
}

As the last wrapper feature, I will show you how to drop a cookie on the user's computer using only one line of code:

C#
DropCookie("MyCookieKey", "Hello World!");

Tada!

Conclusion

I hope you will find the Base Page class a good starting point for your website projects. As a note, this Base Page is just that, "A Base Page"; which means it can be tailored to your requirements or left as is.

Revision History

  • 03/21/2006 - Submitted article.
  • 03/29/2006 - Added Constants class and a Web.config file to the source code.
  • 03/31/2006 - Modified the Base Page to include the "string.IsNullOrEmpty" suggestion by HyperX.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMessage Closed Pin
10-Aug-20 1:41
Oliver_james10-Aug-20 1:41 
Generalwebsite development Pin
Hackr.io26-Jul-18 2:38
professionalHackr.io26-Jul-18 2:38 
GeneralMessage Closed Pin
26-Jan-21 17:48
Ashish1488659426-Jan-21 17:48 
GeneralAn alternative idea Pin
Chris Maunder30-Mar-06 13:33
cofounderChris Maunder30-Mar-06 13:33 
GeneralRe: An alternative idea Pin
Stu Ellerbusch31-Mar-06 1:39
Stu Ellerbusch31-Mar-06 1:39 
GeneralRe: An alternative idea Pin
HurricaneGordon5-Apr-06 8:33
HurricaneGordon5-Apr-06 8:33 
GeneralConstants.Formats & Constants.CookieKey Pin
cptgijoe29-Mar-06 2:07
cptgijoe29-Mar-06 2:07 
GeneralRe: Constants.Formats & Constants.CookieKey Pin
Stu Ellerbusch29-Mar-06 3:01
Stu Ellerbusch29-Mar-06 3:01 
GeneralBase Page Pin
GOHDS21-Mar-06 9:44
GOHDS21-Mar-06 9:44 
GeneralRe: Base Page Pin
Stu Ellerbusch22-Mar-06 1:40
Stu Ellerbusch22-Mar-06 1:40 
GeneralGood Job Pin
newbegin21-Mar-06 6:45
professionalnewbegin21-Mar-06 6:45 
GeneralImprovements To GetParm Method Pin
Kevin Watkins21-Mar-06 6:02
Kevin Watkins21-Mar-06 6:02 
GeneralRe: Improvements To GetParm Method Pin
Stu Ellerbusch21-Mar-06 6:08
Stu Ellerbusch21-Mar-06 6:08 
GeneralRe: Improvements To GetParm Method Pin
RK KL29-Mar-06 16:18
RK KL29-Mar-06 16:18 
GeneralRe: Improvements To GetParm Method Pin
Chris Maunder30-Mar-06 13:32
cofounderChris Maunder30-Mar-06 13:32 
GeneralRe: Improvements To GetParm Method Pin
Kevin Watkins30-Mar-06 22:20
Kevin Watkins30-Mar-06 22:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.