(ASP.NET.C#)appsessings.json

設定情報などコンフィギュレーションを書くところがわからない。
別に新規に作ってもよいのだけれど、何を使って読み込めばいいのかわからない。
別に新規に作ってもよいのだけれど、面倒くさい。

ということで、 appsettings.json に書くことにしました。

// appsettings.json
{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Trace",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "SQLServerConnection": "Server=XXXXX\\SQLEXPRESS;Database=LeaveManagement;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Application": {
    "Mail": {
      "ClientID": "xxxxxxxxxx",
      "ClientSecret": "xxxxxxxxxx",
      "TenantId": "xxxxxxxxxx"
    }
  } 
}
// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();
    
    // appsetting.json読み込み
    services.Configure<AppSettings>(Configuration);  // ←なんなんだ、この Configuration ってのは。コンテクストから言って appsetting.json を表してるのだろうけど。。。
    
    // ・・・
}

appsettings.json をインジェクションできる。これは便利

public EmployeeMstController(IOptions<AppSettings> optionAccessor)
{
    this._appSettings = optionAccessor.Value;
}