1
1

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 3 years have passed since last update.

.NETCoreでアクション別のバージョン管理

Last updated at Posted at 2021-09-10

アクション別のバージョン管理

Microsoft.AspNetCore.Mvc.Versioning」を使用したバージョン管理方法

手順

  1. バージョン管理を行いたいプロジェクトに Microsoft.AspNetCore.Mvc.Versioning を追加

Install-Package Microsoft.AspNetCore.Mvc.Versioning

  1. Startup.cs の ConfigureServices()に以下を追加
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
  // ApiVersioningを追加
  services.AddApiVersioning(options =>
  {
      // クライアントにApiバージョンを通知
      options.ReportApiVersions = true;
      // これがないとクライアント側でエラーが出る
      options.AssumeDefaultVersionWhenUnspecified = true;
      // Apiのデフォルトバージョンを1.0に設定
      options.DefaultApiVersion = new ApiVersion(1, 0);
  });

  services.AddControllers();
}

3.Controllerの名前空間及び関数に属性を追加

HomeController.cs
// サポートしているバージョンを追加
[ApiVersion("1.0")]
[ApiVersion("2.0")]
[Route("v{version:apiVersion}/[controller]")]
public class HomeController : Controller
{
  [HttpGet]
  public string Get() => "v1";

  // Apiバージョンを上書き
  [HttpGet, MapToApiVersion("2.0")]
  public string GetV2() => "v2";
}
  • /v1/Home
    • v1が表示
  • /v2/Home
    • v2が表示
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?