在传统的 ASP 中,我们要用 application 对象去存储应用于整个 application 的变量。这当然会带来内存消耗的代价。在 .net 中,我们可以用 static 变量来改善它,采用 static 变量在大多数时候存储的速度会比 application 对象快。
做法:
创建一个 webApplication ,假设名称为 webApplication1 ,在 Global.aspx 中的 Global 类中增加一个静态的成员 sGreeting.
public class Global : System.Web.HttpApplication
{
public static string sGreeting = "China is great!";
……
}
在 WebForm1中增加一个label,名称为label1.
在 page_load()中为label1的text属性赋值。
private void Page_Load(object sender, System.EventArgs e)
{
Label1.Text = WebApplication1.Global.sGreeting;
}
运行程序后将会看到 China is great!