ASP.NET MVC How to access a property in the Global.asax file from the Controller?
in the Global.asax file, I manage some threads, and - from the Controller - I need to invoke an event of the 开发者_Python百科one's thread. Is it possible to have access to that thread ?
You could use the application state to store some object that will be shared among all users of the application:
protected void Application_Start()
{
Application["foo"] = "bar";
...
}
and inside your controller you can access this property:
public ActionResult Index()
{
var foo = HttpContext.Application["foo"] as string;
...
}
You could if it were any other kind of object, like a string, because you'll need to declare the property as static in the Global.asax
to make it available to the rest of the app:
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; }
}
This will be available to the rest of the application. You can call by doing:
public ActionResult MyAction()
{
var bla = Application.MyProperty;
}
That said, I dont think you want to make a Thread
available to the rest of the app this way.
精彩评论