.net core使用knife4j ui


  在Java项目中有使用knife4j ui来管理接口文档,swagger ui实在太难看了。

  换回到.net core这边还没有很好的一个工具, 在github有搜到有net core版本,https://github.com/luoyunchong/IGeekFan.AspNetCore.Knife4jUI

使用下来有几个问题,首先全局的Authorize菜单里的header不好使,于是我下载了knife4j的源码来debug发现,如果需要正常使用还需要改几个地方

 

一、全局Authorize的参数Key

 

这里我之前的代码默认是Bearer,与前端的实际不符, 可在StatrUp的AddSecurityDefinition里修改这个参数key

c.AddSecurityDefinition("Authorization", new OpenApiSecurityScheme()
                {
                    Description = "JWT授权token前面需要加上字段Bearer与一个空格,如Bearer token",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey,
                    BearerFormat = "JWT",
                    Scheme = "Bearer"
                });

 

二、在调试接口时的Authorization不会自动添加到Header里面去

 

经过源码debug发现api-doc的paths->controllerName->post->security节点没有,于是查找实现方法,后面在https://github.com/dotnet/aspnet-api-versioning

这里发现可以实现IOperationFilter接口来添加

/// <summary>
    /// Represents the Swagger/Swashbuckle operation filter used to document the implicit API version parameter.
    /// </summary>
    /// <remarks>This <see cref="IOperationFilter"/> is only required due to bugs in the <see cref="SwaggerGenerator"/>.
    /// Once they are fixed and published, this class can be removed.</remarks>
    public class SwaggerFilter : IOperationFilter
    {
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            // NOTE: This adds the "Padlock" icon to the endpoint in swagger, 
            //       we can also pass through the names of the policies in the string[]
            //       which will indicate which permission you require.
            operation.Security = new List<OpenApiSecurityRequirement>
            {
                new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Name = "Authorization",
                            Type = SecuritySchemeType.ApiKey,
                            In = ParameterLocation.Header,
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id = "Authorization"
                            },
                         },
                         new string[] { "global" }
                    }
                }
            };
        }
    }

在StartUp添加filter

c.OperationFilter<SwaggerFilter>();

 

三、接口名称相同的会跳转到第一个

作者也给出了解决方案,就是修改CustomOperationIds,这里加个list处理一下,如果有重复的才加controller名称

c.CustomOperationIds(apiDesc =>
                {
                    var controllerAction = apiDesc.ActionDescriptor as ControllerActionDescriptor;
                    if (ActionNameList.Contains(controllerAction.ActionName))
                    {
                        return controllerAction.ActionName + $" ({controllerAction.ControllerName})";
                    }
                    ActionNameList.Add(controllerAction.ActionName);
                    return controllerAction.ActionName ;
                });

四、复制链接路径时,没法路由到具体的接口

这个没时间研究,交给有时间的小伙仔研究一下。

 

至此可以比较正常使用knife4j ui了

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM