记Asp.Net Core Swagger使用并带域接口处理的方法
网络编程 2021-07-04 22:41www.168986.cn编程入门
这篇文章主要介绍了记Asp.Net Core Swagger使用并带域接口处理的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们狼蚁网站SEO优化随着长沙网络推广来一起学习学习吧
引用作者原话:Asp.Net的WebApi中使用Swagger作为说明和测试的页面是非常不错的,比起WebApiTestClient来至少在界面上的很大的提升。但是使用Swagger时如果只是一般的控制器直接放到Controller下就可以了,而如果因不同的业务需求而需要分类或者有同名的类名时时则没办法很好的处理。
因为业务需求需要创建域,但是Swagger并未将域添加到接口。所以需要加上以下操作才行。
安装Swagger方法:
为了大家多看微软官方文档、就直接引用Swagger安装及使用方法。以下是微软官方文档。
增加域接口显示方法:
using Microsoft.AspNetCore.Mvc.ApiExplorer; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace System.Web.Http.Description { /// <summary> /// API描述器扩展 /// </summary> public static class ApiDescriptionExtension { /// <summary> /// 获取区域名称 /// </summary> /// <param name="description"></param> /// <returns></returns> public static List<string> GetAreaName(this ApiDescription description) { string areaName = description.ActionDescriptor.RouteValues["area"]; string controlName = description.ActionDescriptor.RouteValues["controller"]; List<string> areaList = new List<string>(); areaList.Add(controlName); if (!string.IsNullOrEmpty(areaName)) { description.RelativePath = $"{areaName}/{controlName}/{description.RelativePath}"; } return areaList; } } }
通过接口描述扩展获取区域及相关信息进行改写扩展。
使用说明:
services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Version = "v1.0.0", Title = " API", Description = description, TermsOfService = "你的公司", Contact = new Swashbuckle.AspNetCore.Swagger.Contact { Name = "Blog.Core", Email = "Blog.Core@xxx.", Url = "https://.jianshu./u/94102b592a" } }); //使用域描述 c.TagActionsBy(apiDesc => apiDesc.GetAreaName()); var basePath = PlatformServices.Default.Application.ApplicationBasePath; var xmlPath = Path.Combine(basePath, xmlName);//这个就是刚刚配置的xml文件名 c.IncludeXmlComments(xmlPath, true);//默认的第二个参数是false,这个是controller的注释,记得修改 });
红色部分加入代码即可。
结果展示:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程