2011-04-01 76 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

相关问题