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
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