LoginSignup
1
1

More than 5 years have passed since last update.

asp.net core > swagger

Last updated at Posted at 2018-12-07

環境

VS:VisualStudio2017
フレームワーク: .asp.net core 2.1

Swaggerをインストール

「ツール」→「NuGetパッケージマネージャー」→「NuGetパッケージの管理」

検索にSwashbuckle.AspNetCore

image

Startup.cs

Startup.csに追記

  1. using
Startup.cs
using Swashbuckle.AspNetCore.Swagger;

ConfigureServicesにAddSwaggerGenを追加

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

    services.AddSwaggerGen(options =>
    {
        options.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
    });
}

ConfigureにUseSwagger, UseSwaggerUIを追加

Startup.cs

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }


    // SwaggerドキュメントをJSONとして出力する設定
    app.UseSwagger();

    // Swagger UIを表示する設定
    app.UseSwaggerUI(options =>
    {
        options.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
    });



    app.UseMvc();
}

デバッグの設定

デバッグの設定

プロジェクトのプロパティ→デバッグ

ブラウザーの起動に、swagger を入力

image

実行

F5で実行する。

URLの後ろに「/swagger」が付いたURLで起動する。はず。

例)http://localhost:49256/swagger

以下のような画面が表示されれば成功

image

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