.Net Core Web Api_笔记22_Swagger自订文件并读取API注解描述

Swagger刚开始可以将其理解成网页版本的postman

我们可以对其测试发送资料看回传结果

在预设webapi专案产生的WeatherForecastController
新增一个post action

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MyNet5ApiAdoTest.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }

        [HttpPost("Add")]
        public string AddWeatherForecast(WeatherForecast weather)
        {
            if(weather != null)
            {
                DateTime dt = weather.Date;
                string summary = weather.Summary;
                int TemperatureC = weather.TemperatureC;
                int TemperatureF = weather.TemperatureF;
                return "1";
            }
            return "0";
        }



    }
}

在此准备一份json 测试资料尝试post

{
   "date":"2022-06-09T09:49:03.3956842+08:00",
   "temperatureC":9,
   "temperatureF":47,
   "summary":"Hot"
}

https://ithelp.ithome.com.tw/upload/images/20220110/201074526SjpUrv974.png

https://ithelp.ithome.com.tw/upload/images/20220110/20107452A5ZPtJgctr.png

在专案中下中断点

https://ithelp.ithome.com.tw/upload/images/20220110/20107452yh47oAOH3e.png

下方则会呈现回应结果与curl对应呼叫语法转译
https://ithelp.ithome.com.tw/upload/images/20220110/20107452hs6vUg9i58.png

而在Swagger中若我们想自订API文档给其他外部开发者或厂商去做使用
移至我们的Startup.cs的ConfigureServices方法

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddSwaggerDocument(config => {
        config.PostProcess = document =>
        {
            //API版号
            document.Info.Version = "v1.0.1";
            //API名称
            document.Info.Title = "厂房MES API 测试";
            //API描述介绍
            document.Info.Description = "提供原物料资料查询存取";
            //API联络人资讯
            document.Info.Contact = new NSwag.OpenApiContact
            {
                Name = "XXX科技",
                Email = "[email protected]",
                Url = "https://#"
            };
            //API授权许可资讯
            document.Info.License = new NSwag.OpenApiLicense
            {
                Name = "XXX科技 版权所有",
                Url = "https://#"
            };
        };
    });//注册NSwag提供的服务
}

https://ithelp.ithome.com.tw/upload/images/20220110/20107452M7DqTYrHXc.png

而一般API官方文件也会习惯写中英文的注解描述各只API功能
在C#专案我们都知道可透过XML在每个函数、method上方添加功能描述注解

https://ithelp.ithome.com.tw/upload/images/20220110/201074524O5xKw6c97.png

若写好的API这样子呈现别人会不晓得功能与目的

https://ithelp.ithome.com.tw/upload/images/20220110/20107452EQ6OlnMzCG.png

由於预设专案是不会开启产生XML注解并读取机制的
因此要自己去开启

https://ithelp.ithome.com.tw/upload/images/20220110/201074529GL3xi2JNO.png

https://ithelp.ithome.com.tw/upload/images/20220110/201074529N4JzBb1gu.png

本篇已同步发表至个人部落格
https://coolmandiary.blogspot.com/2022/01/net-core-web-api22swagger.html


<<:  根据 NIST SP 800-204通讯 (Communication) 是对基於微服务的应用程序是最为独有的

>>:  资料验证(golang)(Day23)

编写有效的用户故事 (user stories)

如何编写有效的用户故事? 用户故事是一种捕捉早期需求的强大技术。用户故事捕捉了需求的 WHO、WHA...

从 JavaScript 角度学 Python(23) - Class

前言 接下来算是小聊一下 Python 的 Class 语法而已,算是稍微休息一下,所以这边简单聊就...

Day 23 「启动!Outside-In 之路」Controller 与单元测试

台湾的职业运动中,最具代表性的应该就是棒球了。大家有去打击练习场玩过吗?现在的打击练习场,在业者持续...

人脸辨识-day23 资料的型态

在每种资料在影藏些资讯,但会因单位或数值的不同,导致代表的意义也有所不同,有以下的类别,Nomina...

用 Python 畅玩 Line bot - 15:Flex Message(二)

在 bubble 中,每一个区段都可以新增一个 box,可以想像成 html 中的 div,而 bo...