首页 > 其他分享 >.Net Core Web API 版本控制

.Net Core Web API 版本控制

时间:2023-02-27 14:58:43浏览次数:66  
标签:Core 版本控制 version controller API using Net NET versioning

原文:https://briancaos.wordpress.com/2022/04/04/c-net-core-api-versioning-with-microsoft-aspnetcore-mvc-versioning/

 

 

.NET Core allows you to control versions of your APIs. To do so you need the following NuGet package:

But lets try to version our API.

STEP 1: CREATE A CONTROLLER

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 using Microsoft.AspNetCore.Mvc; using System;   namespace Controllers.V1 {   [Route("v{version:apiVersion}/[controller]")]   public class HelloWorldController : Controller   {     [HttpGet]     public async Task<IActionResult> HelloWorld()     {       // This is a stupid example, but just an example.       // You would of course have a real async method here       return await Task.Run(() => Ok("Hello world"));     }   } }

Notice how I in line 6 defines the route as v{version:apiVersion}/[controller]? The {version:apiVersion} will define the versioning that we use later on.

STEP 2: ADD VERSIONING TO THE SERVICES

In your startup code, add the versioning to the services:

1 2 3 4 5 6 7 8 9 10 11 12 using Microsoft.AspNetCore.Mvc.Versioning; using Microsoft.AspNetCore.Mvc;   ... ...   services.AddApiVersioning(   options =>   {     options.ReportApiVersions = true;     options.Conventions.Controller<Controllers.V1.HelloWorldController>().HasApiVersion(new ApiVersion(1, 0));   });

Here we have now defined that the Controllers.V1.HelloWorldController have version 1.

STEP 3: CALL YOUR ENDPOINT

The endpoint will now respond to the following URL:

  • /v1/helloworld

WHY NOT JUST HARDCODING THE API VERSION TO THE CONTROLLER ROUTE?

Versioning have several advantages. This is not all it can do:

You can deprecate versions by adding the following attribute to the controller:

1 2 3 [Route("v{version:apiVersion}/[controller]")] [ApiVersion("1.0", Deprecated = true)] public class HelloWorldController : Controller

You can map a specific method in a controller to a specific version by adding the following attribute to the method. So one controller can have several versions of an endpoint:

1 2 3 [HttpGet] [MapToApiVersion("1.0")] public async Task<IActionResult> HelloWorld()

And you can do a whole lot of other version related stuff. But this will get you started.

Please note that versioning is not supported when using .NET 6 Miminal API’s, but there is a plan to implement it later.

You are now an API versioning expert. Happy coding.

MORE TO READ:

标签:Core,版本控制,version,controller,API,using,Net,NET,versioning
From: https://www.cnblogs.com/panpanwelcome/p/17159635.html

相关文章

  • PANNs: Large-Scale Pretrianed Audio Neural Networks for Audio Pattern Recognitio
    AudioPatternRecognitionincludes:audiotaggingacousticsceneclassificationmusicclassificationspeechemotionclassificationsoundeventdetection.........
  • kubernetes之Ingress发布Dashboard(二)
    1.什么是DashboardDashboard是基于网页的Kubernetes用户界面。你可以使用Dashboard将容器应用部署到Kubernetes集群中,也可以对容器应用排错,还能管理集群资源。你......
  • ASP.NET AJAX使用方法概述(三)
    2.5ASP.NETAJAX使用方法概述完成了上述配置之后,我们就可以立刻开始一次令人激动的ASP.NETAJAX之旅了!不过无论在开始什么样的旅行之前,我们都应该选择好最佳的路线。在上......
  • netapp fas2220更换硬盘的记录
    1、买来新的硬盘,出现broken的问题如下:fas2220>volstatus-rAggregateaggr0(online,raid_dp,reconstruct)(blockchecksums) Plex/aggr0/plex0(online,normal,......
  • net6项目遇到的一些问题
    1添加redis时,在控制台程序可以运行,而在webapi无法运行,同样代码在家里电脑可以可能是网络设置的问题2获取appsetting的值2.1 builder.Configuration["Redis:Host"];2.......
  • 记一次 .NET某家装ERP系统 内存暴涨分析
    一:背景1.讲故事前段时间微信上有一位老朋友找到我,说他的程序跑着跑着内存会突然爆高,有时候会下去,有什么会下不去,怀疑是不是某些情况下存在内存泄露,让我帮忙分析一下,其实......
  • .NET依赖注入之一个接口多个实现
    .NET依赖注入之一个接口多个实现 前言最近又在项目中碰到需要将原本单实现的接口改造成多个实现的场景,这里记录一下常见的几种改法。假设已经存在如下接口ICustomSe......
  • 我做的百度飞桨PaddleOCR .NET调用库
    我做的百度飞桨PaddleOCR.NET调用库 我做的百度飞桨PaddleOCR.NET调用库.NETConf2021中国我做了一次《.NET玩转计算机视觉OpenCV》的分享,其中提到了一个效果特别......
  • 如何将 Autofac 整合进 Net6.0 Core MVC 项目中
    一、前言1、前言Asp.NetCoreMvc,我也用了很长一段时间了,它现在的编程模型和方式还是特别棒的,都是组件开发,什么都可以替换,当然了,您别抬杠,有些还是......
  • Winform C# .Net中给ListBox加ToolTip提示
    1、首先,在工具箱中将ToolTip控件拖入到Winform界面下方。2、接着,在指定的ListBox添加:XXX_MouseMove事件(如已添加则忽略此步)。其中:XXX为您的控件名称。3、在该事件中添加如此......