2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C#でOpen APIのSwaggerにXMLコメントを反映したい

Last updated at Posted at 2023-10-24

1. はじめに

  • C#でOpenAPIのAPIドキュメントにコメントを追加したい
  • コメントはソースコードのXMLコメントを使用したい

2. 開発環境

  • C#
  • .NET 6
  • Visual Studio 2022
  • Windows 11
  • Swashbackle.AspNetCore (NuGet)

3. Swashbackle.AspNetCoreのインストール

  • NugetよりSwashbackle.AspNetCoreをインストールする
    image.png
    * 適用ボタンをクリックする
    image.png

4. プロジェクトのプロパティ変更

  • プロジェクトを右クリックして、プロパティを選択する
    image.png

  • 「ドキュメントファイル > APIドキュメントファイルを含むファイルを生成します。」にチェックをする
    image.png

5. Program.csの修正

Program.cs
// builder.Services.AddSwaggerGen();
builder.Services.AddSwaggerGen(options =>
{
    var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
    options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});

6. 動作確認

  • Controllerに追加したXMLコメントが反映できた
    image.png

7. 参考文献

2
2
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?