0

Created asp.net core web application. In this existing class library converted nuget package and installed. how to assign appsettings key value to class library from asp.net core app.

1 Answer 1

1

From the asp.net docs: https://learn.microsoft.com/de-de/aspnet/core/fundamentals/configuration

Write a Constructor in your StartUp.cs, just like you would do with the 'older' versions of asp.net core.

public class Startup
{
public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();
}

public IConfigurationRoot Configuration { get; set; }

public void ConfigureServices(IServiceCollection services)
{
    // Adds services required for using options.
    services.AddOptions();
 ...

Create a appsettings.json if not already exist.

Just for Example: { "key1" : "value1" }

Read it with Configuration["key1"]

If you need the Configuration in any of your Controller, have a look at the DI in ASP:

https://learn.microsoft.com/de-de/aspnet/core/fundamentals/dependency-injection

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

Comments

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.