【发布时间】:2020-03-04 08:42:49
【问题描述】:
我遵循了这个:
Web Api How to add a Header parameter for all API in Swagger
还有这个:
How to send custom headers with requests in Swagger UI?
但是,这些 IParameter、Parameter 或 NonBodyParameters 均不适用于 ASP .NET CORE 3.1。
我想在我的 swagger 上添加一个标头,该标头采用一个租户 ID,该 ID 最好取自登录用户。
我也经历过:
https://github.com/domaindrivendev/Swashbuckle.AspNetCore
谁能指出我正确的方向?
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>();
}
【问题讨论】:
-
请向我们展示您的代码
-
@JérômeMEVEL 我已经添加了它,但它找不到上面提到的命名空间。这就是我寻找替代方案的原因。
-
我刚刚编辑了我的答案以获取详细信息。考虑到类名是
AddRequiredHeaderParameter,我认为你实际上应该写Required = true。如果我的问题解决了您的问题,请考虑将其标记为已接受的答案(不同于赞成)。
标签: c# asp.net-core swagger