.NET WebApi 配置 Swagger进阶

上一节对接RAP2 配置Swagger 的API写好后,在RAP2平台导入json配置时发现导入失败,JAVA团队写的就OK.

在移动端同事的帮助下,发现JAVA项目写出来的接口,在根节点是有配制Tags属性的(里面配置了控制器信息),而不管是 .NET Framework还是 .Net Core 项目 在根节点 是没有Tags 属性的,因此我就开始了百度谷歌大法。

 "swagger": "2.0",
    "info": {
        "version": "v1",
        "title": "Swagger"
    },
    "host": "localhost:44348",
    
    "tags":[{"name":"order-controller","description":"Order Controller"},{"name":"test-controller","description":"Test Controller"}],
    
    "schemes": [
        "https"
    ],
    .......

.NETCORE

经过搜索发现.Netcore实现比较简单,就是对每个控制器加上Swagger注解

首先引用 Swashbuckle.AspNetCore.Annotations; 注解包 在控制器上加上 SwaggerTag 属性
在这里插入图片描述
然后运行就ok了

访问 https://localhost:44356/swagger/v1/swagger.json 能够看到在根节点出现了Tags属性,在RAP2平台上导入也ok
在这里插入图片描述

.NET Framework

关于 Framework 版的搜索了很久,也没有找到相关介绍,又认认真真看了一下Swagger 官方文档,确实是可以添加的。

最终还得感谢 freecodecamp 的 camperbot 给出了我想要的答案

可访问 https://www.freecodecamp.org/forum/t/arranging-operations-alphabetically-in-swashbuckle-swagger/19350 查看原文解决方案

这里我先新建一个扩展类(主要功能)

namespace TemperatureRecord.Swagger.Extensions //项目名称加扩展名
{
    public class CustomDocumentFilter : IDocumentFilter
    {
        public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, System.Web.Http.Description.IApiExplorer apiExplorer)
        {
            //make operations alphabetic
            var paths = swaggerDoc.paths.OrderBy(e => e.Key).ToList();
            swaggerDoc.paths = paths.ToDictionary(e => e.Key, e => e.Value);
            //controller comments do not get added to swagger docs. This is how to add them.
            AddControllerDescriptions(swaggerDoc, apiExplorer);
        }
        private static void AddControllerDescriptions(SwaggerDocument swaggerDoc, System.Web.Http.Description.IApiExplorer apiExplorer)
        {
            var doc = new TemperatureRecord.Areas.HelpPage.XmlDocumentationProvider(GetXmlCommentsPath());
//项目名称
            List<Tag> lst = new List<Tag>();
            var desc = apiExplorer.ApiDescriptions;
            ILookup<HttpControllerDescriptor, ApiDescription> apiGroups = desc.ToLookup(api => api.ActionDescriptor.ControllerDescriptor);
            foreach (var apiGroup in apiGroups)
            {
                string tagName = apiGroup.Key.ControllerName;
                var tag = new Tag { name = tagName };
                var apiDoc = doc.GetDocumentation(apiGroup.Key);
                if (!String.IsNullOrWhiteSpace(apiDoc))
                    tag.description = apiDoc;
                lst.Add(tag);
            }
            if (lst.Count() > 0)
                swaggerDoc.tags = lst.ToList();
        }
        private static string GetXmlCommentsPath()
        {
            var sr = string.Format(@"{0}\bin\TemperatureRecord.xml",//这里写之前设置输出的XML
                                                  System.AppDomain.CurrentDomain.BaseDirectory);
            return sr;
        }
    }
}

然后在 SwaggerConfig 中引用

    public class SwaggerConfig
    {
        public static void Register()
        {
            var thisAssembly = typeof(SwaggerConfig).Assembly;
            GlobalConfiguration.Configuration
                .EnableSwagger(c =>
                    {
                        c.Schemes(new[] { "http", "https" });
 //引用写好的类                         c.DocumentFilter<TemperatureRecord.Swagger.Extensions.CustomDocumentFilter>(); 
                    
c.SingleApiVersion("v1", "TemperatureRecord");
c.IncludeXmlComments(string.Format(@"{0}\bin\TemperatureRecord.xml",                                          System.AppDomain.CurrentDomain.BaseDirectory));
c.DescribeAllEnumsAsStrings();
                    })
                .EnableSwaggerUi(c =>
                    {
                    });
        }
    }

然后运行项目 访问 http://localhost:44346/swagger/docs/v1

在json 的末尾就有根节点中的Tags了, 在RAP2平台上导入ok
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
.NET Framework是由微软公司开发的一种基于Windows操作系统的应用程序框架。它提供了一个统一的编程模型、一系列预先编写好的类库和工具,帮助开发人员创建和运行各种类型的应用程序。.NET Framework使用的主要编程语言是C#。 C#是一种面向对象的高级编程语言,它结合了C和C++的特点,并添加了一些现代化的特性。C#的语法简洁易懂,容易学习和使用,同时具备很高的灵活性和可扩展性。它支持面向对象编程、事件驱动编程和组件编程,可以用于开发各种类型的应用程序,包括桌面应用程序、Web应用程序和移动应用程序等。 .NET Framework提供了许多重要的功能和特性,包括多语言互操作性、自动垃圾回收、安全性和可靠性等。它还提供了一套完整的类库,包括用于用户界面、数据访问、网络编程、安全性和多媒体等方面的类库,使开发人员可以更快地开发出高效、可靠的应用程序。 此外,.NET Framework还提供了一系列强大的开发工具,包括可视化开发环境Visual Studio和命令行编译器等。它们提供了丰富的开发工具和调试功能,帮助开发人员更快地编写、测试和调试代码。 总之,.NET Framework是为开发Windows平台应用程序而设计的一种强大的框架,它结合了C#编程语言提供了丰富的功能和工具,使开发人员能够更加高效地创建出高质量的应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值