locked
How to make global variables on asp.net core 2 on level of all project web API ? RRS feed

  • Question

  • User696604810 posted

    problem

    How to make global variables on asp.net core 2 on level of all project web API ?

    I work on asp.net core 2.1 project web API 

    i have more controller and i need to use global variables for all solution or project shared CompanyCode value

    so that how to do that on asp.net core 

    so that how to make company code as global variables or session or shared if i call it in any controller i can get values ?

    Sunday, August 18, 2019 11:48 AM

All replies

  • User475983607 posted

    so that how to make company code as global variables or session or shared if i call it in any controller i can get values ?

    I assume you would use standard ASP.NET Core configuration.  The official docs cover the fundamentals.

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2

    From a general language perspective, static class variables are shared by the entire web application. 

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members

    There's cache.

    https://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory?view=aspnetcore-2.2

    And 

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-2.2

    Sunday, August 18, 2019 12:31 PM
  • User1120430333 posted
    You could look into caching the data into an object and getting the data out of the object that was populated from the database.

    https://docs.microsoft.com/en-us/aspnet/core/performance/caching/memory?view=aspnetcore-2.2
    Sunday, August 18, 2019 9:53 PM
  • User711641945 posted

    Hi ahmedbarbary,

    You could create a class for those variables that you need.And set the value in your Startup.cs ConfigureServices method.Then you could use DI to access it in code.Here is a working demo as below:

    1.MDUOptions:

    public class MDUOptions
    {
        public string CompanyCode { get; set; }
    }

    2.Startup.cs:

    public void ConfigureServices(IServiceCollection services)
    {
         Action<MDUOptions> mduOptions = (opt =>
         {
              opt.CompanyCode = "aaaa";
         });
         services.Configure(mduOptions);
         services.AddSingleton(resolver => resolver.GetRequiredService<IOptions<MDUOptions>>().Value);
         services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    3.ValuesController:

    public class ValuesController : ControllerBase
    {
        private static string _mduDb;
        public ValuesController(MDUOptions options)
        {
           _mduDb = options.CompanyCode;
        }
    }

    Best Regards,

    Rena

    Monday, August 19, 2019 8:23 AM
  • User1501655665 posted

    Hi Rena,

    How to set the CompanyCode value dynamically depending on user selection of company upon login ?

    opt.CompanyCode = "aaaa";

    Tuesday, March 24, 2020 12:07 PM
  • User-474980206 posted

    just create a new library project. in that project create a static class, and define the values:

    public static class ApplicationValues
    {
        public const string CompanyCode = "abc";
    } 
    

    add this project to all projects that need it.

    Tuesday, March 24, 2020 4:06 PM