Click here to Skip to main content
Click here to Skip to main content

Base Page Class Website Development

, 29 Mar 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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:

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:

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:

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)

Share

About the Author

Stu Ellerbusch
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralAn alternative idea PinadminChris Maunder30-Mar-06 14:33 
GeneralRe: An alternative idea PinmemberStu Ellerbusch31-Mar-06 2:39 
GeneralRe: An alternative idea PinmemberHurricaneGordon5-Apr-06 9:33 
GeneralConstants.Formats & Constants.CookieKey Pinmembercptgijoe29-Mar-06 3:07 
GeneralRe: Constants.Formats & Constants.CookieKey PinmemberStu Ellerbusch29-Mar-06 4:01 
GeneralBase Page PinmemberGOHDS21-Mar-06 10:44 
GeneralRe: Base Page PinmemberStu Ellerbusch22-Mar-06 2:40 
GeneralGood Job Pinmembernewbegin21-Mar-06 7:45 
GeneralImprovements To GetParm Method PinmemberKevin Watkins21-Mar-06 7:02 
GeneralRe: Improvements To GetParm Method PinmemberStu Ellerbusch21-Mar-06 7:08 
GeneralRe: Improvements To GetParm Method PinmemberHyperX29-Mar-06 17:18 
GeneralRe: Improvements To GetParm Method PinadminChris Maunder30-Mar-06 14:32 
GeneralRe: Improvements To GetParm Method PinmemberKevin Watkins30-Mar-06 23:20 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 29 Mar 2006
Article Copyright 2006 by Stu Ellerbusch
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid