NAlexP
NAlexP

Reputation: 183

API only returns responses with no content

My methods only return responses with no content.

Controller

[HttpGet("Floors/{floorId}", Name = "FloorById")]
public IActionResult GetFloor(int floorId)
{
    try
    {
        Floor floor = _repository.Floor.GetFloor(floorId);
        if (floor == null)
            return NotFound();
        return Ok(floor);
    }
    catch (Exception e)
    {
        return StatusCode(500, "text");
    }
}

Repository

public Floor GetFloor(int floorId)
{
    return _context.Floors.FirstOrDefault(f => f.Id == floorId);
}

Ideally, this code should return an Ok response with the object as well.

Instead, I only get an Ok response when using swagger. Not even the NotFound.

Upvotes: 0

Views: 1004

Answers (1)

Nkosi
Nkosi

Reputation: 247333

Swagger is unable to determine what type the action returns based on the IActionResult.

Use the ProducesResponseType attribute:

[ProducesResponseType(typeof(Floor), 200)] // <-- THIS
[HttpGet("Floors/{floorId}", Name = "FloorById")]
public IActionResult GetFloor(int floorId) {
    try {
        Floor floor = _repository.Floor.GetFloor(floorId);
        if (floor == null)
            return NotFound();
        return Ok(floor);
    } catch (Exception e) {
        return StatusCode(500, "text");
    }
}

Upvotes: 1

Related Questions