当前位置: 首页 > 知识库问答 >
问题:

斜扣ASP。Net Core WebApi:Swagger文档不包括用于版本选择的请求头或查询参数?

梁巴英
2023-03-14

我使用ASP。Net核心WebApi、Swashback和Microsoft。AspNetCore。Mvc。文档版本控制和API版本控制。

到目前为止,版本控制也有效。

我的问题:

生成的Swagger UI文档不包括用于确定endpoint版本的html" target="_blank">参数(请求标头或查询参数)。因此,当我在Swagger文档中按“Execute”时,为endpoint选择了错误的版本(默认版本)。

准确地说:

Swagger执行此请求:https://localhost:5001/values

但是,它应该执行此请求:https://localhost:5001/values?api-版本=2.0

代码:

控制器:

[ApiController]
[Route("[controller]")]
[SwaggerTag("Gets some values. Have fun with it")]
[ApiVersion("1.0")]
[ApiVersion("2.0")]
public class ValuesController : ControllerBase
{
    public ValuesController()
    {
    }

  /// <summary>
  /// Gets all values
  /// </summary>
  /// <remarks>There are values from 1 to 10</remarks>
  /// <returns></returns>
  [HttpGet]
  [SwaggerResponse(200, "Request was successful a list of values was returned", typeof(int[]))]
  [MapToApiVersion("1.0")]
  public async Task<IActionResult> Get()
  {
        return Ok(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
  }

  /// <summary>
  /// Gets all values
  /// </summary>
  /// <remarks>There are values from 1 to 20</remarks>
  /// <returns></returns>
  [HttpGet]
  [SwaggerOperation(Tags = new[] { "Values", "Changed Endpoints" })]
  [SwaggerResponse(200, "Request was successful a list of values was returned", typeof(int[]))]
  [MapToApiVersion("2.0")]
  public async Task<IActionResult> Getv2()
  {
        return Ok(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 });
  }

启用版本控制:

        services.AddApiVersioning(config =>
        {
            config.DefaultApiVersion = new ApiVersion(1, 0);
            config.AssumeDefaultVersionWhenUnspecified = true;
            config.ReportApiVersions = true;

            config.ApiVersionReader = ApiVersionReader.Combine(new QueryStringApiVersionReader(),
             new HeaderApiVersionReader()
             {
                 HeaderNames = { "x-api-version" }
             });
        });

启用SwaggerGen:

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("1.0", new OpenApiInfo
            {
                Title = "API v1.0",
                Version = "1.0",
            });
            c.SwaggerDoc("2.0", new OpenApiInfo
            {
                Title = "API v1.0",
                Version = "1.0",
            });
            c.EnableAnnotations();
            c.IncludeXmlComments(System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), "wwwroot", "OpenApi.xml"));
            c.DocInclusionPredicate((docName, apiDesc) =>
            {
                if (!apiDesc.TryGetMethodInfo(out MethodInfo methodInfo)) return false;

                var versions = methodInfo.GetCustomAttributes(true)
                    .OfType<Microsoft.AspNetCore.Mvc.MapToApiVersionAttribute>()
                    .SelectMany(attr => attr.Versions).ToList();

                return versions.Any(v => v.ToString() == docName);
            });
        });

有人能帮我吗?

共有1个答案

葛泳
2023-03-14

通过添加以下内容解决了此问题:

     services.AddVersionedApiExplorer(options =>
        {
            options.GroupNameFormat = "'v'VVV";
            options.DefaultApiVersion = new ApiVersion(1, 0);
            options.AssumeDefaultVersionWhenUnspecified = true;
            options.DefaultApiVersionParameterDescription = "Do NOT modify api-version!";
        });

现在添加了参数。但遗憾的是,没有自动填充默认值。有人有主意吗?

 类似资料:
  • 我试图使用jpa组件来选择从标题中获取的id。我在文档中找到了一个使用本机select查询的示例: 我试图用${header.id}替换“1”常量: 这似乎不管用,我明白了: 也许还有别的方法可以让它发挥作用?

  • 问题内容: 我有一个名为faq_questions的表,其结构如下: 我试图建立一个给定排序顺序的查询,选择具有最高排序顺序的行。 例子: 好的,假设我为已知的排序顺序(id 4)输入了5,我需要它返回ID为3的行。由于不能保证sort_order是连续的,所以我不能只选择known_sort_order + 1。 谢谢! 问题答案: 似乎太简单了,但是看起来像您所需要的:

  • 我有两个版本的oracle,oracle Database 11g Enterprise Edition 11.2.0.1.0版本-生产版和oracle Database 11.g Enterprise Edition版本11.2-0.3.0版本-64位生产版 我有这个sql: 它适用于11.2.0.1.0,但在11.2.0.3.0上,我收到此错误: 我能做些什么来解决这个问题? 感谢。

  • 下面使用Siteminder筛选器, @override protected void configure(HttpSecurity http)引发异常{http.AddFilterBefore(siteminderFilter(),RequestHeAderAuthenticationFilter.Class).........} @bean public RequestHeaderAuthen

  • 我对请求/请求类型/标头/正文没有任何控制。 我正在创建一个servlet,它必须处理一个POST请求,该请求将在请求url中包含多个反斜杠。(例如www.servlet.com/path/foo1/foo2/foo3/foo4/foo5) 我想捕获值"fo1/fo2/fo3/fo4/fo5"和反斜线的数量可能会有所不同。以下解决方案将适用于捕捉一个“foo”,但一旦有更多,它将达到404。 {资