Home > Enterprise >  .net core 6 api bad request
.net core 6 api bad request

Time:09-30

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?

CodePudding user response:

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

  • Related