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:
- Handle unexpected errors that have occurred during a page request.
- Wrapper functions for getting request parameter values, and dropping browser cookies.
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:
- Log the ASPX page (and date-time).
- Log the exception message.
- Log the stack trace.
- 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.
- 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.
- 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:
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)
if (Request.QueryString[parm] != null && Request.QueryString[parm].Length > 0)
if (Session[parm] != null && Convert.ToString(Session[parm]).Length > 0)
if (Request.Cookies[parm] != null && Request.Cookies[parm].Value.Length > 0)
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!");
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.
- 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.