ASP.NET MVC How to access a property in a Global.asax file using a controller?

in the Global.asax file, I control some threads, and - from the controller - I need to trigger a single thread event. Is it possible to have access to this stream?

+4
source share
2 answers

You can use the state of the application to store some object that will be used by 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; ... } 
+9
source

You could if it were some other object, like a string, because you need to declare the property as static in Global.asax to make it available to the rest of the application:

 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 for the rest of the application. You can call by doing:

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

However, I donโ€™t think you want to make Thread available to the rest of the application this way.

+3
source

Source: https://habr.com/ru/post/1346217/


All Articles