Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading