ASP.NET - C# 应用程序环境背景






4.63/5 (10投票s)
2005年6月27日
2分钟阅读

68423

416
演示如何通过利用继承在 ASP.NET 应用程序中显示背景图片。
引言
您或应用程序的用户有多少次认为您正在 ASP.NET 应用程序的开发模式下,但实际上却在生产模式下? 这可能是一个代价高昂的错误,特别是如果有人修改了数据。 大多数开发软件的公司都有多个应用程序环境,例如:开发、认证“测试”、演示和生产。 开发人员、质量保证分析师、用户等了解他们所处的环境至关重要。 您可能会认为,每个人都应该知道这一点,因为他们访问特定的 URL 或它显示在登录屏幕上。 猜猜怎么了,我们都是人,并且经常会忘记我们处于生产模式,因为开发和生产中的用户界面之间几乎没有差异。
本文演示了在您的 ASP.NET 应用程序中展示应用程序环境是多么容易。 通过利用继承,应用程序中的每个 WebForm 都可以从自定义基类页面派生。 这允许代码在一个位置编写,并通过不编写每个页面来输出环境来减少冗余。 BasePage
类将渲染一个背景图片,并将应用程序环境作为背景重复显示。 BasePage
类使用静态 AppConfig
类从 Web.config 文件中获取 ApplicationEnvironment
变量。 Web.config 文件包含一个 appSetting
节点。 我向 appSettings
添加了一个自定义的 application_environment
节点。 本例中的 application_environment
模式是
- 开发 - "
DEV
" - 认证 - "
CERT
" - 演示 - "
DEMO
" - 生产 - "
PROD
"
BasePage
仅为 DEV
、CERT
和 DEMO
渲染背景“水印”图片。 我们不希望在生产环境中看到背景。
代码
如上所述,代码非常简单。 它由两个类组成。 BasePage
类和 AppConfig
类。 希望显示背景的 WebForms 从 BasePage
继承。
BasePage
此类重写了 Render
方法,并根据应用程序环境输出背景图片。
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Text;
namespace DevEnvWebApp
{
/// <summary>
/// Description: BaseClass all webforms can derive from.
/// This example is given in C#. This class
/// demonstrates the simplicity to view the
/// current application environment state.
///
/// Programmer: Kenny Young
/// E-mail: ken.g.young@gmail.com
/// </summary>
public class BasePage : System.Web.UI.Page
{
public BasePage()
{
}
/// <summary>
/// Returns the current application environment's state.
/// </summary>
protected string ApplicationEnvironment
{
get
{
try
{
AppConfig config = AppConfig.GetInstance;
return config.ApplicationEnvironment.ToUpper();
}
catch
{
return "";
}
}
}
/// <summary>
/// Override the System.Web.UI.Page's Render method. This method
/// outputs a backsplash depending on the application environment.
/// </summary>
/// <PARAM name="writer">HtmlTextWriter</PARAM>
protected override void Render(HtmlTextWriter writer)
{
string appEnv = this.ApplicationEnvironment;
try
{
string backImage = "";
switch(appEnv)
{
// Are we in production mode?
case "PROD" :
base.Render(writer);
return;
// Are we in development mode?
case "DEV" :
backImage = "/images/dev.gif";
break;
// Are we in certification mode?
case "CERT":
backImage = "/images/cert.gif";
break;
// Are we in demo mode?
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);
// Find the body tag and anything inbetween
// the greater than sign.
Match match = Regex.Match(sb.ToString(),
@"(<[oO][dD][yY](.*?)>)" );
// The result.
string result = match.Value;
string resultBgColor = Regex.Match(result,
@"([gG][cC][oO][lL][oO][rR][=][" "](.*?)[""])").Value;
string newResult = "";
// Did we find a bgcolor in the string?
if(resultBgColor.Length > 0)
{
// Replace it with our version.
newResult = result.Replace(resultBgColor, "");
}
newResult = result.Substring(0, result.Length - 1);
newResult = newResult + sbWater.ToString() + ">";
sb.Replace(result, newResult);
// Remove the BgColor.
sb.Replace("bgColor", "bggColor");
// Output the new html.
Response.Write(sb.ToString());
}
catch(Exception ex)
{
string err = ex.Message;
}
}
}
}
WebForm - 派生类
此类从 BasePage
派生。 它自动继承渲染背景图片的功能。 任何您想显示背景的 WebForm 都应该从 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
{
/// <SUMMARY>
/// Summary description for WebForm1.
/// </SUMMARY>
public class MainForm : BasePage
{
protected System.Web.UI.WebControls.Button btnClickMe;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <SUMMARY>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </SUMMARY>
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 类
此类是一个静态类,充当到 Web.config 文件的接口。 它具有从 Web.config XML 中的 appSettings
节点检索值的属性。 应用程序环境存储在配置文件中。
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
包含一个 appSettings
部分,允许自定义变量。 应用程序环境作为 application_environment
键存储在这里。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="application_environment" value="CERT"/>
</appSettings>
<system.web>
屏幕截图
下面是在网页上看到的背景的示例。 应用程序模式为开发 - DEV
。
另一个 WebForm,其应用程序模式为开发 - DEV
。
以下是第一个截图的示例,但在认证模式下 - CERT
。
就是这样! 一个被忽视问题的简单解决方案。
尽情享用!