在Global.asax中店名单 [英] store List in Global.asax

查看:166
本文介绍了在Global.asax中店名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以存储应用水平字符串像一个Global.asax文件:
全球ASAX:

We can store application level strings in a global.asax file like: Global asax:

void Application_Start(object sender, EventArgs e) 
    {
        Application.Lock();
        Application["msg"] = "";            
        Application.UnLock();        
    }

然后在页面我们得到了味精变量:
a.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
 {
        string msg = (string)Application["msg"];
        //manipulating msg..
 }

不过,我想存储对象的应用程序级变量,而不是string信息的列表我试过这样:
的Global.asax:

void Application_Start(object sender, EventArgs e) 
    {
        Application.Lock();
        List<MyClassName> myobjects= new List<MyClassName>();      
        Application.UnLock();        
    }

a.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
    {
        //here I want to get myobjects from the global.asax and manipulate with it..
    }

因此​​,如何存储列表myobjects在Global.asax的应用程序级的变量,并与这方面的工作?

此外,我还有一个问题:
如何任何通知发送给客户端(浏览器)时,在Global.asax中的全局变量发生改变?

Moreover, I have another question: How to send any notification to clients(browsers) when global variable in global.asax is changed?

推荐答案

的一种方法是将其保存到缓存:

One way would be to store it into the cache :

using System.Web.Caching;    

protected void Application_Start()
{
   ...
   List<MyClassName> myobjects = new List<MyClassName>();
   HttpContext.Current.Cache["List"] = myobjects;
}

然后访问/操纵它:

then to access / manipulating it :

using System.Web.Caching;

var myobjects =  (List<MyClassName>)HttpContext.Cache["List"];
//Changes to myobjects
...
HttpContext.Cache["List"] = myobjects;

这篇关于在Global.asax中店名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆