• 记Asp.Net Core Swagger 使用 并带域接口处理


    引用作者原话:Asp.Net的WebApi中使用Swagger作为说明和测试的页面是非常不错的,比起WebApiTestClient来至少在界面上的很大的提升。但是使用Swagger时如果只是一般的控制器直接放到Controller下就可以了,而如果因不同的业务需求而需要分类或者有同名的类名时时则没办法很好的处理。

      因为业务需求需要创建域,但是Swagger 并未将域添加到接口。所以需要加上以下操作才行。

    安装Swagger 方法:

        为了大家多看微软官方文档、就直接引用Swagger安装及使用方法。 以下是微软官方文档。

    https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-2.1&tabs=visual-studio

    增加域接口显示方法:

     

    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.com", Url = "https://www.jianshu.com/u/94102b59cc2a" }
                        
                    });
                    //使用域描述
                    c.TagActionsBy(apiDesc => apiDesc.GetAreaName());
    
                    var basePath = PlatformServices.Default.Application.ApplicationBasePath;
                    var xmlPath = Path.Combine(basePath, xmlName);//这个就是刚刚配置的xml文件名
                    c.IncludeXmlComments(xmlPath, true);//默认的第二个参数是false,这个是controller的注释,记得修改
                });

    红色部分加入代码即可。

    结果展示:

     

    本文章参考 学而时习之进行 更改。  多谢!
    作者:_学而时习之_
    来源:CSDN
    原文:https://blog.csdn.net/xxdddail/article/details/81868721
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    SqlServer 查看被锁的表和解除被锁的表
    Windows Server 2012 R2 或 2016 无法安装 .Net 3.5.1
    请求文件下载URL过长处理
    T4语法
    windows下 安装 rabbitMQ 及操作常用命令
    ubuntu系统启动qtceator时提示:Qt5.5.1/Tools/QtCreator/lib/qtcreator/plugins/libHelp.so: 无法加载库
    升级到VS2013常见问题
    Windowns 无法启动 Office Software Protection Platform 服务,系统找不到指定的文件
    SVN clean失败解决方法
    使用PostSharp在.NET平台上实现AOP
  • 原文地址:https://www.cnblogs.com/dwjkj/p/10499825.html
Copyright © 2020-2023  润新知