简体   繁体   中英

Model not displaying in swagger UI .NET

A model class userQuery that I have written isn't showing up in Swagger UI. I have referenced it in my controller file so I expected it to show up in the Swagger UI. I use SwashBuckle. What am I missing here?

The controller file having an endpoint:

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:

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 .

You can use three different parameters: Id, DateCreated, and DateUpdated in your controller and use the [FromQuery] attribute for each of them.

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. But usually, you shouldn't use the body for a GET request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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