Click here to Skip to main content
11,641,222 members (65,449 online)
Click here to Skip to main content

ASP.NET - C# Application Environment Backsplash

, 6 Jul 2005 54.5K 400 54
Rate this:
Please Sign up or sign in to vote.
Demonstrates how to diplay a backsplash image in an ASP.NET application by taking advantage of inheritance.

Introduction

How often have you or the users of the application thought you were in development mode of your ASP.NET application but were actually in production? This could be a very costly mistake, especially if someone modifies data. Most companies developing software have multiple application environments such as: Development, Certification "Testing", Demonstration, and Production. It is very critical that developers, quality assurance analysts, users, etc. know what environment they are in. You might think, everyone should know this because they go to a specific URL or it is displayed on the login screen. Guess what, we are all human and often can forget we are in production because there is little variation between the user interfaces in development and production.

This article demonstrates how easy it is to demonstrate the application environment in your ASP.NET applications. By taking advantage of inheritance, each WebForm in the application can derive from a custom base page. This allows the code to be written in one location and reduces redundancy by not coding every page to output the environment. The BasePage class will render a backsplash image with the application environment repeated as the background. The BasePage class utilizes a static AppConfig class to get the ApplicationEnvironment variable from the Web.config file. The Web.config file contains an appSetting node. I added a custom application_environment node to the appSettings. The application_environment modes in this example are:

  1. Development - "DEV"
  2. Certification - "CERT"
  3. Demonstration - "DEMO"
  4. Production - "PROD"

The BasePage only renders a backsplash "watermark" image for DEV, CERT, and DEMO. We do not want to see a backsplash while in production.

The Code

As stated above, the code is quite simple. It consists of two classes. The BasePage class and the AppConfig class. WebForms that want to display the backsplash inherit from the BasePage.

BasePage

This class overrides the Render method and outputs a backsplash image dependent upon the application environment.

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Text;

namespace DevEnvWebApp
{
    /// <span class="code-SummaryComment"><summary>
</span>

WebForm - Derived Class

This class derives from the BasePage. It automatically inherits the functionality to render the backsplash image. Any WebForm you want to display the backsplash should derive from BasePage.

using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace DevEnvWebApp
{
    /// <span class="code-SummaryComment"><SUMMARY>
</span>

AppConfig Class

This class is a static class that acts as an interface to the Web.config file. It has properties to retrieve the values from the appSettings node in the Web.config XML. The Application Environment is stored in the config file.

using System; 
using System.Configuration; 
using System.Collections; 
using System.Text; 
using System.Xml; 

namespace DevEnvWebApp 
{ 
    public class AppConfig 
    { 
        public static AppConfig GetInstance = new AppConfig(); 
        private const string APP_NAME = "application_name"; 
        private const string APPLICATION_ENVIRONMENT = "application_environment"; 
        private Hashtable collection = null; 

        private AppConfig() 
        { 
            try 
            { 
                collection = new Hashtable(); 

                foreach (string key in 
                  ConfigurationSettings.AppSettings.AllKeys) 
                { 
                    collection.Add(key, 
                      ConfigurationSettings.AppSettings[key]); 
                } 
            } 
            catch (Exception e) 
            { 
                throw new Exception("Exception caught " + 
                         "in AppConfig constructor.", e); 
                
            } 
        } 

        public string ApplicationName 
        { 
            get
            {
                return GetValue(APP_NAME); 
            }
        } 

        public string ApplicationEnvironment
        { 
            get
            {
                return GetValue(APPLICATION_ENVIRONMENT); 
            }
        } 

        public string GetValue(string key) 
        { 
            try 
            { 
                return collection[key].ToString(); 
            } 
            catch (Exception e) 
            { 
                throw new Exception("Exception caught" + 
                           " in AppConfig.GetValue", e); 
            } 
        } 
    } 
}

Web.config appSettings

Contains an appSettings section that allows custom variables. The application environment is stored here as the application_environment key.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="application_environment" value="CERT"/>
    </appSettings>
    <system.web>

Screenshots

Below is an example of the backsplash seen on the web pages. The application mode is Development - DEV.

Backsplash - DEV

Another WebForm with an application mode of Development - DEV.

Backsplash - DEV

Below is an example of the first screenshot but in Certification mode - CERT.

Backsplash - DEV

That's it! A simple solution to an overlooked problem.

Enjoy!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Kenny Young
United States United States
Ken currently works as the Director of Software Engineering at the Pediatrics Epidemiology Center at the University of South Florida. He is involved in the architecture and design on numerous clinical trial projects. Some of the project include:

1. Rare Diseases Clinical Research Network
2. TrialNet - Diabetes
3. Teddy - Diabetes
4. CCOP - Cancer
5. AIDA - Diabetes

You may also be interested in...

Comments and Discussions

 
QuestionHttpModule implementation? Pin
Mike Ellison6-Jul-05 13:38
memberMike Ellison6-Jul-05 13:38 
AnswerRe: HttpModule implementation? Pin
Kenny Young7-Jul-05 4:06
memberKenny Young7-Jul-05 4:06 
GeneralGreat - and maybe you can use machine.comfig Pin
Mark Focas27-Jun-05 13:06
memberMark Focas27-Jun-05 13:06 
GeneralRe: Great - and maybe you can use machine.comfig Pin
Kenny Young27-Jun-05 13:28
memberKenny Young27-Jun-05 13:28 
GeneralGreat article Pin
jdeer27-Jun-05 9:55
memberjdeer27-Jun-05 9:55 
GeneralRe: Great article Pin
Kenny Young27-Jun-05 13:31
memberKenny Young27-Jun-05 13:31 

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
Web03 | 2.8.150731.1 | Last Updated 6 Jul 2005
Article Copyright 2005 by Kenny Young
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid