Global.asax中的应用程序变量在ASP.NET中返回null值

时间:2015-08-20 12:57:51

标签: c# asp.net

我创建了一个非常小的应用程序,它在网页上打印活动会话的数量。 global.asax.cs代码如下所示

namespace WebApp
{
public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        Application["TotalOnline"] = 0;
    }
    protected void Session_Start(object sender, EventArgs e)
    {
        Application["TotalOnline"] = (int)Application["TotalOnline"] + 1;
    }
    protected void Session_End(object sender, EventArgs e)
    {
         Application["TotalOnline"] = (int)Application["TotalOnline"] - 1;
    }
}
}

hell.aspx.cs代码如下所示

namespace WebApp
{
    public partial class hell : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int a = (int)Application["TotalOnline"];
            Response.Write("The no of users online are "+ a );
        }
    }
}

hell.aspx来源如下

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="hell.aspx.cs" Inherits="WebApp.hell" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>

当我尝试在firefox中运行时给出

的NullReference异常
 int a = (int)Application["TotalOnline"];

我不明白为什么.....请帮助我...提前谢谢..

0 个答案:

没有答案