首页 > 其他分享 >314 API Versions 01(UrlSegmentApiVersionReader)

314 API Versions 01(UrlSegmentApiVersionReader)

时间:2024-06-22 10:29:14浏览次数:12  
标签:WebAPI 01 UrlSegmentApiVersionReader CitiesController API context cs using publi

示例

1、准备两个版本的CitiesController.cs

删除不在需要的TestController.cs

Controllers文件夹下新建v1文件夹,将CitiesController.cs移动到v1中,弹出的对话框点击OK和Yes;

新建v2文件夹,拷贝一份CitiesController.cs,命名空间改成v2;

v2 CitiesController.cs如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using CitiesManager.WebAPI.DatabaseContext;
using CitiesManager.WebAPI.Models;

namespace CitiesManager.WebAPI.Controllers.v2
{

    public class CitiesController : CustomControllerBase
    {
        private readonly ApplicationDbContext _context;

        public CitiesController(ApplicationDbContext context)
        {
            _context = context;
        }

        // GET: api/Cities
        /// <summary>
        /// To get list of cities (including cityID nad city name) from 'cities' 
table
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        //[Produces("application/xml")]
        public async Task<ActionResult<IEnumerable<string?>>> GetCities()
        {
            var cities = await _context.Cities.OrderBy(c => c.CityName).Select(c 
=> c.CityName).ToListAsync();

            return cities;
        }
    }
}

2、启用API版本

安装NuGet包

<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.1.0" 
/>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" 
Version="5.1.0" />

Program.cs中添加AddApiVersionin()

builder.Services.AddControllers(options =>
{
    options.Filters.Add(new ProducesAttribute("application/json")); //Response 
Body
    options.Filters.Add(new ConsumesAttribute("application/json")); //Request 
Body
}).AddXmlSerializerFormatters();

builder.Services.AddApiVersioning(config => {
    config.ApiVersionReader = new UrlSegmentApiVersionReader();
});

更新CustomControllerBase.cs路由

[Route("api/v{version: apiVersion}/[controller]")]
[ApiController]
public class CustomControllerBase : ControllerBase
{

}

Controller添加ApiVersion Attribute

namespace CitiesManager.WebAPI.Controllers.v1
{
    [ApiVersion("1.0")]
    public class CitiesController : CustomControllerBase

结果

可以调用不同版本的Controller

Gitee获取源码:

https://gitee.com/huang_jianhua0101/asp.-net-core-8.git

标签:WebAPI,01,UrlSegmentApiVersionReader,CitiesController,API,context,cs,using,publi
From: https://blog.csdn.net/KevinHuang2088/article/details/139843712

相关文章

  • 316 API Versions 03(Enabling API Versions in Swagger)
    更新Program.cs添加两个版本的SwaggerDoc//Swaggerbuilder.Services.AddEndpointsApiExplorer();//generatesdescriptionforallendpointsbuilder.Services.AddSwaggerGen(options=>{options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory,"api.x......
  • 预约挂号系统微信小程序(30127)
     有需要的同学,源代码和配套文档领取,加文章最下方的名片哦一、项目演示项目演示视频二、资料介绍完整源代码(前后端源代码+SQL脚本)配套文档(LW+PPT+开题报告)远程调试控屏包运行三、技术介绍Java语言SSM框架SpringBoot框架Vue框架JSP页面Mysql数据库IDEA/Eclipse开发四、项......
  • 二手交易网站微信小程序(30140)
     有需要的同学,源代码和配套文档领取,加文章最下方的名片哦一、项目演示项目演示视频二、资料介绍完整源代码(前后端源代码+SQL脚本)配套文档(LW+PPT+开题报告)远程调试控屏包运行三、技术介绍Java语言SSM框架SpringBoot框架Vue框架JSP页面Mysql数据库IDEA/Eclipse开发四、项......
  • Android14之java层:增加系统API(二百二十)
    简介:CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长!优质专栏:Audio工程师进阶系列【原创干货持续更新中……】......
  • 【单片机毕业设计选题24019】-基于STM32的安防监测灭火系统
    系统功能:1.水泵喷水灭火功能:当火焰传感器监测到火焰时,蜂鸣器报警,水泵工作实现灭火。2.风扇功能:当烟雾传感器检测到CO或温度传感器检测到温度超过阈值时,蜂鸣器报警,启动风扇进行驱散烟雾或降温。3.防盗功能介绍:采用红外传感器,当有人靠近时就会报警。4.通过蓝牙实现在......
  • 【单片机毕业设计选题24018】-基于STM32和阿里云的农业大棚系统
    系统功能:系统分为手动和自动模式,上电默认为自动模式,自动模式下系统根据采集到的传感器值自动控制,温度过低后自动开启加热,湿度过高后自动开启通风,光照过低后自动开启补光,水位过低后自动开启水泵补水。手动模式下可以手动控制加热/除湿/补光/补水。主要功能模块原理图:......
  • 代码随想录算法训练营第十四天 | 226.翻转二叉树 101.对称二叉树 104.二叉树的最大深
    226.翻转二叉树题目:给你一棵二叉树的根节点root,翻转这棵二叉树,并返回其根节点。解题:思路:遍历的过程中交换每个节点的左右孩子。选择哪种遍历方式?中序不行,左中右,左边子节点交换完,处理中间交换了左节点和右节点,再处理右节点去交换时这个右节点就是原来的左节点,所以有一边就一......
  • 01、Shell 编程规范与变量
    目录1.1Shell脚本概述1.1.1Shell的作用        1.1.2编写第一个Shell脚本        1.1.3重定向与管道操作1.重定向操作2.管道操作1.2Shell变量的作用、类型1.2.1自定义变量1.定义新的变量2.查看和引用变量的值3.变量赋值的特殊操作4.设置变......
  • 深度学习--tensorflow中操作张量的高频率api--87
    目录1.创建张量2.shape操作3.数学运算4逻辑运算5.张量之间的操作6.数据类型的转换7.聚合(规约)操作8argmax1.创建张量tf.constant(value,dtype=None,shape=None,name='Const')tf.zeros(shape,dtype=tf.float32,name=None)tf.ones(shape,dtype=tf.float32,name......
  • python web框架哪家强?Flask、Django、FastAPI对比
    前言当你掌握了python的基础知识,并且会用和HTML和CSS编写简单的静态网页。现在你只需再掌握一个pythonweb框架的知识,就可以开始编写一个动态的网站了。目前市面比较流程的pythonweb框架有三个flask、Django、FastAPI。接下来我们对比一下。他们三个各自有什么特点。Flas......