Pete
Pete

Reputation: 58462

.net core 6 api bad request

In the past I used to do my api requests like such

    [HttpPost]
    public IActionResult CreateLead(CreateLeadRequest request)
    {
        if (request == null)
        {
            return BadRequest();
        }

        return Ok(_handler.Value.CreateLead(request));
    }

But now with .net 6 you return the actual value instead of an action result:

    [HttpPost("create", Name = nameof(CreateLead))]
    public async Task<int> CreateLead(CreateLeadRequest request)
    {
        return await _handler.Value.CreateLead(request);
    }

So how do I return the bad result for null request in this case as the compiler complains that the BadRequest isn't an int?

Upvotes: 2

Views: 1703

Answers (1)

klekmek
klekmek

Reputation: 573

You can use async Task<ActionResult<int>>. This allows you to return HttpStatuscodes as well as the object itself.

Upvotes: 2

Related Questions