Core3.0 升级swagger 配置区别

Nuget 安装包区别

Core2.2

 

 

 Core3.0

 

 

 

 

 

 

 

 

 

 

IDocumentFilter区别

Core2.2

 

 

 

 

 

 

Core3.0

 

 

 

Core 2.2

app.UseSwagger(c =>
{
  c.PreSerializeFilters.Add((swaggerDoc, httpReq) => swaggerDoc.Host = httpReq.Host.Value);
});

  

Core 3.0

app.UseSwagger(c =>
{
  c.PreSerializeFilters.Add((swagger, httpReq) =>
  {
    swagger.Servers = new List<OpenApiServer> { new OpenApiServer { Url = $"{httpReq.Scheme}://{httpReq.Host.Value}" } };
  });
});

 

原使用到的info改变为OpenApiInfo

Microsoft.OpenApi 代替 Swashbuckle.AspNetCore.Swagger

Core 2.2

 

 

Core 3.0

 

 

Swagger 添加 token Header区别

Core 2.2

public class AddAuthTokenHeaderParameter : IOperationFilter
    {
        public void Apply(Operation operation, OperationFilterContext context)
        {
            if (operation.Parameters == null)
                operation.Parameters = new List<IParameter>();

            var attrs = context.ApiDescription.ActionDescriptor.AttributeRouteInfo;

            //先判断是否是匿名访问,
            var descriptor = context.ApiDescription.ActionDescriptor as ControllerActionDescriptor;
            if (descriptor != null)
            {
                //var isAuthorized = descriptor..Select(filterInfo => filterInfo.Instance).Any(filter => filter is IAuthorizationFilter);

                var actionAttributes = descriptor.MethodInfo.GetCustomAttributes(inherit: true);

                bool isAnonymous = actionAttributes.Any(a => a is AllowAnonymousAttribute);
                bool isAuthorized = actionAttributes.Any(a => a is AuthorizeAttribute);
                //非匿名的方法,链接中添加accesstoken值
                if (!isAnonymous && isAuthorized)
                {
                    operation.Parameters.Add(new NonBodyParameter()
                    {
                        Name = "Authorization",
                        In = "header",//query header body path formData
                        Type = "string",
                        Default = "",
                        Required = true //是否必选
                    });
                }

            }
        }
    }

  Core 3.0

public class AddAuthTokenHeaderParameter : IOperationFilter
    {
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            if (operation.Parameters == null)
            {
                operation.Parameters = new List<OpenApiParameter>();
            }

            var attrs = context.ApiDescription.ActionDescriptor.AttributeRouteInfo;

            //先判断是否是匿名访问,
            var descriptor = context.ApiDescription.ActionDescriptor as ControllerActionDescriptor;
            if (descriptor != null)
            {
                //var isAuthorized = descriptor..Select(filterInfo => filterInfo.Instance).Any(filter => filter is IAuthorizationFilter);

                var actionAttributes = descriptor.MethodInfo.GetCustomAttributes(inherit: true);

                bool isAnonymous = actionAttributes.Any(a => a is AllowAnonymousAttribute);
                bool isAuthorized = actionAttributes.Any(a => a is AuthorizeAttribute);
                //非匿名的方法,链接中添加accesstoken值
                if (!isAnonymous && isAuthorized)
                {
                    operation.Parameters.Add(new OpenApiParameter()
                    {
                        Name = "Authorization",
                        In= ParameterLocation.Header,//query header body path formData
                        Description="Token验证",
                         Schema = new OpenApiSchema() { 
                            Type="string",
                         },
                        //In = "header",//query header body path formData
                        //Type = "string",
                        //Default = "",
                        Required = true //是否必选
                    });
                }

            }
        }
    }

  

posted @ 2019-10-11 15:02  在路上的白羊  阅读(2338)  评论(9编辑  收藏  举报