Answers for "declare global variable in startup file in .net core api"

C#
0

declare global variable in startup file in .net core api

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;
    }
}
Posted by: Guest on June-25-2021

Code answers related to "declare global variable in startup file in .net core api"

C# Answers by Framework

Browse Popular Code Answers by Language