.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