在Swagger中为ASP .NET Core 3.1添加标头过滤器 [英] Add a filter for a header in Swagger for ASP .NET Core 3.1

查看:110
本文介绍了在Swagger中为ASP .NET Core 3.1添加标头过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经遵循了:

Web Api如何为Swagger中的所有API添加Header参数

和这个:

如何在Swagger中发送带有请求的自定义标头用户界面?

但是,这些IParameter,Parameter或NonBodyParameters都不在ASP .NET CORE 3.1上工作.

However, none of these IParameter, Parameter or NonBodyParameters work on ASP .NET CORE 3.1.

我想在我的招摇头上添加一个标头,该标头带有一个租户ID,最好从登录的用户那里获取.

I would like to add a header on my swagger which takes a tenant-ID that is preferably taken from the logged user.

我也经历了这个:

https://github.com/domaindrivendev/Swashbuckle.AspNetCore

有人能指出我正确的方向吗?

Can anyone point me to the right direction?

using Swashbuckle.AspNetCore.Swagger;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.JsonPatch.Operations;
using Microsoft.OpenApi.Models;

namespace Intent2.Auth.Utils
{
    public class AddRequiredHeaderParameter : IOperationFilter
    {
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            
            if (operation.Parameters == null)
                operation.Parameters = new List<IParameter>();

            operation.Parameters.Add(new NonBodyParameter
            {
                Name = "X-User-Token",
                In = "header",
                Type = "string",
                Required = false
            });
        }
    }
}
services.AddSwaggerGen(options =>
{
    options.OperationFilter<AddRequiredHeaderParameter>();
}

推荐答案

通过与ASP.NET Core 3.1兼容的最新版本的Swashbuckle, Microsoft.OpenApi.Models中的许多类型已被等效类型替换命名空间.因此,您不应再使用诸如 NonBodyParameter IParameter 之类的类型.这两个都已由单个类 OpenApiParameter 代替.

With the latest version of Swashbuckle compatible with ASP.NET Core 3.1 many types have been replaced by equivalent types in the Microsoft.OpenApi.Models namespace. So you shouldn't use anymore types like NonBodyParameter or IParameter. Both of these have been replaced by a single class OpenApiParameter.

您的代码应如下所示

using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Collections.Generic;

namespace Intent2.Auth.Utils
{
    public class AddRequiredHeaderParameter : IOperationFilter
    {
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {

            if (operation.Parameters == null)
                operation.Parameters = new List<OpenApiParameter>();

            operation.Parameters.Add(new OpenApiParameter()
            {
                Name = "X-User-Token",
                Description = "Access Token",
                In = ParameterLocation.Header,
                Schema = new OpenApiSchema() { Type = "String" },
                Required = true,
                Example = new OpenApiString("Tenant ID example")
            });
        }
    }
}

然后在启动时,像往常一样简单地注入SwaggerGen

Then in your startup, simply inject SwaggerGen as usual

services.AddSwaggerGen(options =>
{
    options.OperationFilter<AddRequiredHeaderParameter>();
}

例如,您甚至可以使租户ID像配置文件一样来自外部.为此,请按照以下步骤修改您的 AddRequiredHeaderParameter :

You can even make the Tenant ID coming from the outside like a configuration file for example. To do that, modify your AddRequiredHeaderParameter as follow

using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;
using System;
using System.Collections.Generic;

namespace Intent2.Auth.Utils
{
    public class AddRequiredHeaderParameter : IOperationFilter
    {
        private string _tenantIdExample;

        public AddRequiredHeaderParameter(string tenantIdExample)
        {
            if (string.IsNullOrEmpty(tenantIdExample )) 
                throw new ArgumentNullException(nameof(tenantIdExample ));

            _tenantIdExample = tenantIdExample;
        }

        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {

            if (operation.Parameters == null)
                operation.Parameters = new List<OpenApiParameter>();

            operation.Parameters.Add(new OpenApiParameter()
            {
                Name = "X-User-Token",
                Description = "Access Token",
                In = ParameterLocation.Header,
                Schema = new OpenApiSchema() { Type = "String" },
                Required = true,
                Example = new OpenApiString(_tenantIdExample)
            });
        }
    }
}

然后从启动阶段开始调用它

And call it that way from your startup

services.AddSwaggerGen(options =>
{
    options.OperationFilter<AddRequiredHeaderParameter>("Tenant ID example");
}

我认为如果您的班级叫做 AddRequiredHeaderParameter ,则应该实际设置 Required = true 而不是 false

By the way I think if your class is called AddRequiredHeaderParameter you should actually set Required = true instead of false

这篇关于在Swagger中为ASP .NET Core 3.1添加标头过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆