2011-04-01 78 views

回答

9

你可以使用應用程序的狀態存儲將應用程序的所有用戶之間共享一些對象:

protected void Application_Start() 
{ 
    Application["foo"] = "bar"; 
    ... 
} 

和你的控制器中你可以訪問此屬性:

public ActionResult Index() 
{ 
    var foo = HttpContext.Application["foo"] as string; 
    ... 
} 
3

你如果它是任何其他類型的對象,如字符串,因爲您需要在Global.asax中聲明該屬性爲靜態,以使其可用於應用程序的其餘部分:

public class Application : HttpApplication 
{ 
    // This is the class declared in Global.asax 

    // Your route definitions and initializations are also in here 

    public static string MyProperty { get; set; } 
} 

這將提供給其他應用程序。您可以通過執行撥打:

public ActionResult MyAction() 
{ 
    var bla = Application.MyProperty; 
} 

這麼說,我不認爲你想一個Thread以這種方式,其餘的應用程序可用。

+0

太棒了。我喜歡這個分配好於將它存儲爲索引+1 – ppumkin 2017-06-28 07:29:25

相關問題