zoukankan      html  css  js  c++  java
  • Asp.Net Core2.0 WebAPI 使用Swagger生成漂亮的接口文档

    1、引用NuGet:

         Swashbuckle.AspNetCore.Swagger

         Swashbuckle.AspNetCore.SwaggerGen

      <PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="1.1.0" />
      <PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="1.1.0" />

    2、Startup文件引用Swagger

     1 services.AddSwaggerGen(options =>
     2 {
     3     options.SwaggerDoc("v1", new Info
     4     {
     5         Version = "v1",
     6         Title = "WebApi文档"
     7     });
     8     options.OperationFilter<ApiHttpHeaderFilter>();
     9 
    10     //Determine base path for the application.  
    11     var basePath = PlatformServices.Default.Application.ApplicationBasePath;
    12     //Set the comments path for the swagger json and ui.  
    13     var xmlPath = Path.Combine(basePath, "WebApi.xml");
    14     options.IncludeXmlComments(xmlPath);
    15 });
    1  app.UseSwagger();
    2  app.UseSwaggerUI(c =>
    3  {
    4      c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApi文档");
    5  });

    3、对需要进行授权登录的接口生成对应的文档输入框,如Bearer Token   

     1 using Microsoft.AspNetCore.Authorization;
     2 using Swashbuckle.AspNetCore.Swagger;
     3 using Swashbuckle.AspNetCore.SwaggerGen;
     4 using System.Collections.Generic;
     5 using System.Linq;
     6 
     7 namespace Core.WebApi.Filters
     8 {
     9     /// <summary>
    10     /// 对需要进行授权登录的接口生成对应的文档输入框,如Bearer Token
    11     /// </summary>
    12     public class ApiHttpHeaderFilter : IDocumentFilter, IOperationFilter
    13     {
    14         /// <summary>
    15         /// 
    16         /// </summary>
    17         /// <param name="operation"></param>
    18         /// <param name="context"></param>
    19         public void Apply(Operation operation, OperationFilterContext context)
    20         {
    21             operation.Parameters = operation.Parameters ?? new List<IParameter>();
    22             var actionAttributes = context.ApiDescription.ActionAttributes();
    23             var allowAnonymous = actionAttributes.Any(a => a.GetType() == typeof(AllowAnonymousAttribute)); // 查询是否过滤权限
    24             if (!allowAnonymous)
    25             {
    26                 operation.Parameters.Add(new BodyParameter
    27                 {
    28                     Name = "Authorization",
    29                     @In = "header",
    30                     Description = "access token",
    31                     Required = true
    32                 });
    33             }
    34         }
    35 
    36         /// <summary>
    37         /// 
    38         /// </summary>
    39         /// <param name="swaggerDoc"></param>
    40         /// <param name="context"></param>
    41         public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
    42         {
    43             throw new System.NotImplementedException();
    44         }
    45     }
    46 }

       引用该类:

    options.OperationFilter<ApiHttpHeaderFilter>();

    4、点击项目右键属性(勾选生成WebApi.xml文档):

      

    效果:

  • 相关阅读:
    聊聊、Highcharts 动态数据
    聊聊、Zookeeper Linux 启动
    聊聊、Zookeeper 客户端 Curator
    聊聊、Zookeeper 客户端 ZkClient
    聊聊、Zookeeper API
    聊聊、Zookeeper 数据结构和操作命令
    聊聊、Java 网络编程
    《Mysql 索引
    《Mysql 事务
    《Mysql 一条 SQL 更新语句是如何执行的?(Redo log)》
  • 原文地址:https://www.cnblogs.com/NuoYer/p/8252023.html
Copyright © 2011-2022 走看看