0

I wrote a simple controller and need to pass optional string parameter division. I make it optional by adding ? in HttpGet attribute. If I pass an argument (in url or in Swagger) then it works fine. Unfortunately, if I don't pass anything then empty list is returned. On top of that, Swagger requires this parameter to execute even though it's optional. Parameter names are the same in attribute and controller. I also initialized parameter with null in my method. How to fix it?

        [HttpGet("all/{division?}", Name = "GetAll")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public async Task<ActionResult<List<AllClientsVm>>> GetAllClients(string division = null)
        {
            var getAll = new GetAllListQuery() { Division = division };
            var dtos = await _mediator.Send(getAll);
            return Ok(dtos);
        }

        public class GetAllListQuery : IRequest<List<AllClientsVm>>
        {
            public string Division { get; set; }
        }
mustafa00
  • 751
  • 1
  • 7
  • 28

0 Answers0