See how appsettings.json works

Page update date :
Page creation date :

Environment

Visual Studio
  • Visual Studio 2019
ASP.NET Core
  • 3.0
  • 3.1

At first

When you create a new ASP.NET Core project, you can create appsettings.json and appsettings. The development.json configuration file is Included in the project.

What is set here is the parameter set toappsettings.json. At the time of development, the parameters of the same key are appsettings. The overwritten settings in Development.json are used.

image

In this article, I'd like to see how they are switched and loaded.

Edit to see how it works

Try adding parameters to each configuration file to see how it works. Add a TestObject section to each one and add theTestValue parameter.

The appsettings.json file is written with shift-JIS character code, so if you want to include Japanese, Save it again in UTF-8.

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "TestObject": {
    "TestValue": "Root です。",
    "Comment": "日本語を使用する場合は appsettings.json を UTF-8 で保存しなおすこと。"
  }
}

Appsettings. Development.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "TestObject": {
    "TestValue": "Development です。",
    "Comment": "日本語を使用する場合は appsettings.json を UTF-8 で保存しなおすこと。"
  }
}

Gets the parameters of the configuration file in the Index action of the HomeController.cs. There is a method of obtaining by specifying the class, but i love it because it is not this subject.

private readonly ILogger<HomeController> _logger;
private readonly IConfiguration _configuration;

// DI で IConfiguration を受け取る
public HomeController(ILogger<HomeController> logger, IConfiguration configuration)
{
  _logger = logger;
  _configuration = configuration;
}

public IActionResult Index()
{
  // appsettings.json から値を取得します。
  // 階層化されている場合は GetSection を使用します。
  ViewData["Message"] = _configuration.GetSection("TestObject").GetValue<string>("TestValue");
  return View();
}

If you create, bind, and load a class, you will find code similar to the following:

// クラス定義
public class TestObject
{
  public string TestValue { get; set; }
  public string Comment { get; set; }
}

// 読み込み
var testObject = _configuration.GetSection(nameof(TestObject)).Get<TestObject>();

Displays the value obtained in Index.cshtml on the view side.

<p>appsettings.json から取得したテキストを表示</p>
<p>@ViewData["Message"]</p>

Debug run locally

Build the project and debug it and try to run the appsettings. You can see that the development.json settings are displayed.

image

Development is the reason why environment variables are loaded when you run an app with ASP.NET Core. The parameter set to ASPNETCORE_ENVIRONMENT as the parameter is appsettings. XXXX.json A configuration file that matches the XXXX portion is now loaded.

Develop is set when debugging, which is the debug tab for project properties. You can see that it is set in the environment variable.

image

If you try to change this value to a different value and debug it, I think that it is confirmed that the development side is not loaded and the root side is loaded.

image

By the way, this parameter is not separated by Debug or Release, so if you debug with Release, The development parameters are used. Debug and Release can't be separated, as in web.config, but you can register multiple profiles. You can take action, such as checking the release settings by switching there.

Deploy and run in IIS

So what happens if you deploy and run it in IIS? Try actually creating a site in IIS and deploying the program.

image

This is the route setting slated to be used. So if you're deploying a program to IIS, you don't have to worry about develop settings.

image

You can load the configuration file at run time if you have any value in the ASPNETCORE_ENVIRONMENT environment variable on the IIS side. Open the configuration editor from the site settings.

image

From the section, select aspNetCore in system.webServer.

image

Select ApplicationHost.config from the location.

image

Open the environmentVariables collection.

image

Add a collection.

image

Type ASPNETCORE_ENVIRONMENT in name, and then type the name of the configuration file you want to load into value.

image

If you restart the site and view the Web screen, you can see that you are loading the target configuration file.

image

Summary

If the initial configuration is the case, the appsettings. If the Development.json settings are loaded and deployed in a production environment such as IIS, I think it's ok to think that appsettings.json will be loaded.

You only have Development when you create a project, but you can create more than one appsettings, and you can change the Development part to any name. If you have more than one execution location, for example, you can create multiple locations, and then set up and operate ASPNETCORE_ENVIRONMENT for your environment.