简体   繁体   English

Model 未显示在 swagger UI .NET

[英]Model not displaying in swagger UI .NET

A model class userQuery that I have written isn't showing up in Swagger UI.我编写的 model userQuery用户查询未显示在 Swagger UI 中。 I have referenced it in my controller file so I expected it to show up in the Swagger UI.我在我的 controller 文件中引用了它,所以我希望它会出现在 Swagger UI 中。 I use SwashBuckle.我使用 SwashBuckle。 What am I missing here?我在这里想念什么?

The controller file having an endpoint:具有端点的 controller 文件:

using Project.Models;

namespace Project.Controllers
{
    [HttpGet]
    [Authorize(Policy = "Read-Run")]
    [Route("byRoute/{element}")]        
    [Produces(typeof(EntityResult<EntityResponse>))]
    public async Task<IActionResult> ListEntities([FromQuery] userQuery entityMatch, string element)
    {
        return Ok((await _entityService.ListEntities(entityMatch, element)));
    }

}

Model class: Model class:

using System;

namespace Project.Models
{
    public class UserQuery
    {
        public int Id { get; set; }
        public DateTime? DateCreated { get; set; }
        public DateTime? DateUpdated { get; set; }        
    }
}

It seems you can't bind complex objects to query string parameters in Swashbuckle (or OpenAPI): check this question .您似乎无法将复杂对象绑定到 Swashbuckle(或 OpenAPI)中的查询字符串参数: 检查此问题

You can use three different parameters: Id, DateCreated, and DateUpdated in your controller and use the [FromQuery] attribute for each of them.您可以在 controller 中使用三个不同的参数:Id、DateCreated 和 DateUpdated,并为每个参数使用 [FromQuery] 属性。

Your endpoint will look like this:您的端点将如下所示:

public async Task<IActionResult> ListEntities(
    [FromRoute] string element,
    [FromQuery] int id, 
    [FromQuery] DateTime? dateCreated,
    [FromQuery] DateTime? DateUpdated
)

Or you can pass the object in the body of the request and use the [FromBody] attribute.或者您可以在请求正文中传递 object 并使用 [FromBody] 属性。 But usually, you shouldn't use the body for a GET request.但通常,您不应该将正文用于 GET 请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM