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:
- Development - "
DEV
"
- Certification - "
CERT
"
- Demonstration - "
DEMO
"
- 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
{
public class BasePage : System.Web.UI.Page
{
public BasePage()
{
}
protected string ApplicationEnvironment
{
get
{
try
{
AppConfig config = AppConfig.GetInstance;
return config.ApplicationEnvironment.ToUpper();
}
catch
{
return "";
}
}
}
protected override void Render(HtmlTextWriter writer)
{
string appEnv = this.ApplicationEnvironment;
try
{
string backImage = "";
switch(appEnv)
{
case "PROD" :
base.Render(writer);
return;
case "DEV" :
backImage = "/images/dev.gif";
break;
case "CERT":
backImage = "/images/cert.gif";
break;
case "DEMO":
backImage = "/images/demo.gif";
break;
}
StringBuilder sbWater = new StringBuilder();
sbWater.Append(" style=\"");
sbWater.Append("background-attachment: scroll;" +
" background-image: url(" +
this.Request.ApplicationPath +
backImage + "); background-repeat: " +
"repeat; background-color: transparent;\"");
System.Text.StringBuilder sb =
new System.Text.StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
HtmlTextWriter newWriter = new HtmlTextWriter(sw);
base.Render(newWriter);
Match match = Regex.Match(sb.ToString(),
@"(<[oO][dD][yY](.*?)>)");
string result = match.Value;
string resultBgColor = Regex.Match(result,
@"([gG][cC][oO][lL][oO][rR][=][""](.*?)[""])").Value;
string newResult = "";
if(resultBgColor.Length > 0)
{
newResult = result.Replace(resultBgColor, "");
}
newResult = result.Substring(0, result.Length - 1);
newResult = newResult + sbWater.ToString() + ">";
sb.Replace(result, newResult);
sb.Replace("bgColor", "bggColor");
Response.Write(sb.ToString());
}
catch(Exception ex)
{
string err = ex.Message;
}
}
}
}
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
{
public class MainForm : BasePage
{
protected System.Web.UI.WebControls.Button btnClickMe;
private void Page_Load(object sender, System.EventArgs e)
{
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.btnClickMe.Click += new System.EventHandler(this.btnClickMe_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnClickMe_Click(object sender, System.EventArgs e)
{
Response.Redirect("AnotherForm.aspx");
}
}
}
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.
="1.0" ="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
.
Another WebForm with an application mode of Development - DEV
.
Below is an example of the first screenshot but in Certification mode - CERT
.
That's it! A simple solution to an overlooked problem.
Enjoy!