Tony
Tony

Reputation: 12695

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 one's thread. Is it possible to have access to that thread ?

Upvotes: 5

Views: 10626

Answers (2)

Sergi Papaseit
Sergi Papaseit

Reputation: 16174

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.

Upvotes: 3

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

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;
    ...
}

Upvotes: 13

Related Questions