Background, I want to develop a new API using ASP.NET Core 7 and using controllers and I want to use best practices to return typed results to simplify the documentation for example.
If I want to return a typed result from an API, I can do:
[HttpGet()]
public ActionResult<List<Products>> GetAllProducts()
{
var beers = repository.GetAllProducts();
return beers;
}
but, my question is if I want to return and use one of the derived classes from IResult instead (for consistency reasons), then the following below will not work, because Results requires two return types.:
[HttpGet()]
public Results<Products> GetAllProducts()
{
var beers = repository.GetAllProducts();
return beers;
}
Sure, I could do a hack like this:
[HttpGet()]
public Results<Products,BadRequest> GetAllProducts()
{
var beers = repository.GetAllProducts();
return beers;
}
But that sounds a bit silly.
What is the recommended type to result if I want to use IResult and I only want to return one type? Or is ActionResult<List> the only option?
I feel that to get a bit more clean code and be modern that I want to stick with IResult based types and not mix with IActionResult.
Is this what you need
[HttpGet()]
public IResult GetAllProducts()
{
var beers = repository.GetAllProducts();
return Results.Ok(beers);
}