How to get application settings in static classes in Aspnet Core 1.1

Asked

Viewed 43 times

0

In my application’s web project I have the appsettings.json:

{
   "Foo": "Exemplo"
},

And I have a class that reflects that configuration:

public class FooSettings
{
    public string Foo { get; set; }
}

In the startup.Cs i read and record to use via dependency injection:

public void ConfigureServices(IServiceCollection services)
{   
    ...
    services.AddOptions();
    services.Configure<FooSettings>(Configuration.GetSection("Foo")); //lê e injeta dependênc
    ...
}

Everything’s OK so far. I can get an instance of Foosettings via dependency injection into builders and it works smoothly.

The question is: And to get in a static class? In Asp.Net MVC 5 it was like this:

public static class Exemplo
{
    private static readonly string Foo = ConfigurationManager.AppSettings.Get("Foo");
}

Configurationmanager took it straight from the web.config

How to do this in Asp.Net Core 1.1?

  • Have you tried using a static constructor?

  • @Rodolphosa I didn’t even know it existed. But unfortunately these constructors can’t have parameters.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.