Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a global variable in ASP.NET Core Web API application?

How can I create a global variable in an ASP.NET Core Web API application? In ASP.NET MVC, I could do it like:

Application["<variableName>"] = <value>

I tried the same in my web API application, but was unable to find an equivalent for this. I saw some solutions which suggested me to store the data appsettings.json, but since the data I want to store in the global variable is not static, I cannot use that setup. I need to set different data during runtime. How can I do that? Please help me.

like image 787
Arjun D Nair Avatar asked Mar 23 '26 05:03

Arjun D Nair


1 Answers

somewhere in project

public static class Config{
       public static Dictionary<string,string> Application = new Dictionary<string,string>();
}

elsewhere

Config.Application["froop"] = "noodle";

of course you will have to deal with race conditions, etc

like image 105
pm100 Avatar answered Mar 25 '26 19:03

pm100



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!