SwashBuckle Swagger-UI具有FromQuery属性的HTTP GET方法的示例请求

问题描述 投票:0回答:1

我已经设法通过POST方法的SwashBuckle.AspNetCoreSwashbuckle.AspNetCore.Filters向我的Web API添加示例:

DTO

public class ExampleDTO
{
    public string MyFoo { get; set; }
}

示例请求

public class ExampleDTOExample : IExamplesProvider<ExampleDTO>
{
    public ExampleDTO GetExamples()
    {
        return new ExampleDTO()
        {
            MyFoo = "bar"
        };
    }
}

控制器方法

[SwaggerOperation(
    Summary = "...",
    Description = "...",
    OperationId = "PostFoo"
)]
[SwaggerResponse(200, "Returns ...", typeof(int))]
[HttpPost]
[Route("post-foo")]
public ActionResult<int> PostFoo([FromBody]ExampleDTO request)
{
    throw new NotImplementedException();
}

此工作正常。当我单击“试用”按钮时,我将“ bar”作为属性foo的预填充值。

但是,当我尝试对GET请求执行相同的操作时,例如,使用类似查询中的参数,则文本框未预填充值“ bar”:

enter image description here

public class ExampleDTO
{
    [FromQuery(Name = "foo")]
    public string MyFoo { get; set; }
}

控制器方法

[SwaggerOperation(
    Summary = "...",
    Description = "...",
    OperationId = "GetFoo"
)]
[SwaggerResponse(200, "Returns ...", typeof(int))]
[HttpGet]
[Route("get-foo")]
public ActionResult<int> GetFoo([FromQuery]ExampleDTO request)
{
    throw new NotImplementedException();
}

如何强制文本框预先填充示例值?到目前为止,我已经找到一种解决方案,用于指定不需要的默认值。我只想在Swagger UI中使用属性作为默认值。

c# asp.net-core asp.net-web-api swagger swashbuckle
1个回答
0
投票

如果我没记错的话,您会看到以下值:enter image description here

不是示例,而是默认值。

...让我寻找过去做过的事...(即将更新)

© www.soinside.com 2019 - 2024. All rights reserved.