How do I access Configuration in any class in ASP.NET Core?

前端 未结 11 1705
梦如初夏
梦如初夏 2020-11-29 01:09

I have gone through configuration documentation on ASP.NET core. Documentation says you can access configuration from anywhere in the application.

Below is Startup.c

11条回答
  •  误落风尘
    2020-11-29 01:26

    There is also an option to make configuration static in startup.cs so that what you can access it anywhere with ease, static variables are convenient huh!

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    
    internal static IConfiguration Configuration { get; private set; }
    

    This makes configuration accessible anywhere using Startup.Configuration.GetSection... What can go wrong?

提交回复
热议问题