简体   繁体   中英

.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 ?

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

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