Consider the following two pieces of code. Both return data to a Web API Get call. Both return a list of items. Both work. The first one was taken from Visual Studio starter Blazor Wasm App. The second one was taken from an online tutorial. tblTitles is a table in a remote database, accessed through _dataContext.
Which of these should be used and why? Or perhaps one suits better for a specific situation?
[HttpGet]
//First method:
public IEnumerable<TitlesTable> Get()
{
var titles = _dataContext.tblTitles.ToList();
return titles;
}
//Second method:
public async Task<IActionResult> Get()
{
var titles = await _dataContext.tblTitles.ToListAsync();
return Ok(titles);
}
I believe you're noticing the different available controller return types. From that docs page:
ASP.NET Core offers the following options for web API controller action return types:
- Specific type
IActionResult
ActionResult<T>
The page offers considerations of when to use each.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With