2

アプリケーションレベルの文字列は、次のようにglobal.asaxファイルに保存できます 。Globalasax:

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

そして、ページでは、「msg」変数を次のように取得します 。a.aspx.cs:

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

ただし、オブジェクトのリストを文字列msgではなくアプリケーションレベルの変数として保存したいと思います。私はこれを試しました: 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..
    }

では、List myobjectsをアプリケーションレベルの変数としてglobal.asaxに格納し、これを操作するにはどうすればよいでしょうか。

さらに、別の質問があり ます。global.asaxのグローバル変数が変更されたときにクライアント(ブラウザー)に通知を送信するにはどうすればよいですか?

4

1 に答える 1

1

1つの方法は、それをキャッシュに保存することです。

using System.Web.Caching;    

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

次に、それにアクセス/操作します:

using System.Web.Caching;

var myobjects =  (List<MyClassName>)HttpContext.Cache["List"];
//Changes to myobjects
...
HttpContext.Cache["List"] = myobjects;
于 2012-10-12T11:24:44.737 に答える