ASP.NET Core

Configuration Execution Order in ASP.NET Core App

Configuration Execution Order in ASP.NET Core App

Configuration Execution Order in ASP.NET Core App

 The default orders in which the various configuration sources are read for the same key are as follows:
1. appsettings.json
2. appsettings.{Environment}.json here we use appsettings.Development.json
3. User secrets
4. Environment Variables


Add a Key in appsettings.Development.json file

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "MyCustomKey": "MyCustomKey Value coming from appsettings.Development.json"
}



Register in Program.cs file to show value of this custom key

public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);
        var app = builder.Build();

        ConfigurationManager configuration = builder.Configuration;
        string MyCustomKeyValue = configuration.GetValue<string>("MyCustomKey");
        app.MapGet("/", () => $"{MyCustomKeyValue}");
       
        app.UseStaticFiles();
        app.UseRouting();
        app.Run();
    }
    }
}


Run your application



launchsSettings.json
Now add the same key as “MyCustomKey”: “MyCustomKey Value coming from Environment Variable of launchsSettings.json” in the IIS Express Profile Section of the launchSettings.json file
{
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "ancmHostingModel": "OutOfProcess",
      "MyCustomKey": "MyCustomKey Value coming from Environment Variable of launchSettings.json file"
    }
  },
}


Run your application










Related Post

About Us

Community of IT Professionals

A Complete IT knowledgebase for any kind of Software Language, Development, Programming, Coding, Designing, Networking, Hardware and Digital Marketing.

Instagram