LoginSignup
11
7

More than 1 year has passed since last update.

【.NET】設定ファイル(例 appsettings.json)にコメント書けるってよ

Last updated at Posted at 2023-01-14

はじめに

.NET Framework で作成した自社製ライブラリーを、.NET 7 に移植することにした。
.NET Framework では設定ファイルは、App.config (XML 形式)を使用していたが、.NET(旧名 .NET Core)からは、appsettings.json(JSON 形式)が推奨されている。

JSON になることで気になったのは、JSON ってコメントが書けたっけ?

そういえば、Visual Studio Code の設定ファイル(settings.json)は、コメント使えますもんね。

調査

公式にコメントをサポートしていることが分かった。

appsettings.json のコメント
appsettings.json および appsettings.{Environment}.json ファイル内のコメントは、JavaScript または C# スタイルのコメントを使用した場合にサポートされています。

確認

下記の App.config を JSON に変更する。

App.config
<?xml version="1.0" encoding="utf-8"?>
<DBInfo>
    <!-- DBサーバー -->
    <DataSource>localhost</DataSource>

    <!-- データベース名 -->
    <Database>test</Database>

    <!-- ユーザー -->
    <DBUserID>admin</DBUserID>

    <!-- パスワード -->
    <DBPassword>P@ssw0rd</DBPassword>
</DBInfo>

パターン1

コメント付きにした JSON ファイルを作成する。
コメントは設定行の上側に設置、/* */// でコメントを記述してみました。
コードのシンタックスハイライトには、jsonc で指定してみた。

appsettings.json
{
  /* -----------------------------
    データベース接続情報 
   -----------------------------*/
  "dbInfo": {
    // DBサーバー
    "server": "localhost",
    // データベース名
    "dbName": "test",
    // ユーザー
    "user": "admin",
    // パスワード
    "password": "P@ssw0rd"
  }
}

プログラム1

※設定ファイル名はappsetting.jsonにしていますが、 違うファイル名でも問題ありません。

using Microsoft.Extensions.Configuration;

class Program
{
    static void Main(string[] args)
    {
        var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile(path: "appsettings.json");
        var configuration = builder.Build();

        IConfigurationSection section = configuration.GetSection("dbInfo");
        string? str = section["server"];
        Console.WriteLine(str);
        str = section["dbName"];
        Console.WriteLine(str);
        str = section["user"];
        Console.WriteLine(str);
        str = section["password"];
        Console.WriteLine(str);
        Console.ReadLine();
    }
}

結果1

localhost
test
admin
P@ssw0rd

パターン2

コメントのパターンを設定行の上側か右側に設置、/* */// でコメントを記述してみました。

appsettings.json
{
  /* -----------------------------
    データベース接続情報 
   -----------------------------*/
  "dbInfo": {
    /* DBサーバー */
    "server": "localhost",
    // データベース名
    "dbName": "test",
    "user": "admin",       /* ユーザー */
    "password": "P@ssw0rd" // パスワード
  }
}

プログラム2

コメントには関係しないが、設定情報の取得方法をパターン1と変えてみました。

using Microsoft.Extensions.Configuration;

class Program
{
    static void Main(string[] args)
    {
        var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile(path: "appsettings.json");
        var configuration = builder.Build();

        string? str = configuration["dbInfo:server"];
        Console.WriteLine(str);
        str = configuration["dbInfo:dbName"];
        Console.WriteLine(str);
        str = configuration["dbInfo:user"];
        Console.WriteLine(str);
        str = configuration["dbInfo:password"];
        Console.WriteLine(str);
        Console.ReadLine();
    }
}

結果2

localhost
test
admin
P@ssw0rd

最後に

.NETの設定ファイル(appsetting.json)のコメントについて検索してみたんですが、あまり言及している日本での記事が見当たらなかったので、書いてみました。

参照

11
7
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
7