20

I am developing a web application in ASP.NET Core and currently have a large set of keys, such as stripe account keys. Instead of having them spread throughout the project in different classes I would like to place them all together in json where they could be accessed globally. I have tried placing them in appsettings.json but cannot access them anywhere.

2
  • 2
    Putting them in appsettings is a great place for this kind of thing (remember to add that file to your gitignore!) To access them in the controllers you need to use DI. Commented Oct 4, 2018 at 20:02
  • Try putting in appsettings.json and then create a class which will help in storing those values Commented Oct 4, 2018 at 20:03

2 Answers 2

23

I often do this kind of thing with connection strings and other global constants. First create a class for those variables that you need. In my project it is MDUOptions but whatever you want.

public class MDUOptions
{
    public string mduConnectionString { get; set; }
    public string secondaryConnectionString { get; set; }
}

Now in your Startup.cs ConfigureServices method:

Action<MDU.MDUOptions> mduOptions = (opt =>
{
    opt.mduConnectionString = Configuration["ConnectionStrings:mduConnection"];
});
services.Configure(mduOptions);
services.AddSingleton(resolver => resolver.GetRequiredService<IOptions<MDUOptions>>().Value);

Now you use DI to access it in code:

public class PropertySalesRepository : IPropertySalesRepository
{
    private static string _mduDb;

    public PropertySalesRepository(MDUOptions options)
    {
        _mduDb = options.mduConnectionString;
    }
    ....
}

In my case the only property I wanted was the string but I could have used the entire options class.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks @nurdyguy, this answer is very clear! Just one follow up, how do I get the MDUOptions parameter you use in the PropertySalesRepository constructor?
The dependency injection built in to the framework will populate the options variable. The line services.AddSingleton(...) is critical for this.
Here is ore info about Dependency Injection, it is MAGIC! learn.microsoft.com/en-us/aspnet/core/fundamentals/…
@nurdyguy why not creating a static class instead?
@OffirPe'er Do you mean a static class with a bunch of hardcoded string values? What happens when you have different values for different environments (dev vs qa vs prod)? Using the IOptions is the best way to do this kind of thing, though the implementation has changed a bit in the last 2 years: learn.microsoft.com/en-us/dotnet/api/…
|
19

In appsettings.json keep the variables.

{
    "foo": "value1",
    "bar": "value2",
}

Create AppSettings class.

public class AppSettings
{
    public string foo { get; set; }

    public string bar { get; set; }
}

In Startup.cs file register.

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.Configure<AppSettings>(Configuration);
}

Usage,

public class MyController : Controller
{
    private readonly IOptions<AppSettings> _appSettings;

    public MyController(IOptions<AppSettings> appSettings)
    {
        _appSettings = appSettings;
    }
    var fooValue = _appSettings.Value.foo;
    var barValue = _appSettings.Value.bar;
}

1 Comment

How are you declaring variables inside of a class?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.