Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning data from a WEB API in a Blazor App

Tags:

c#

webapi

blazor

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);
    }
like image 248
user3656651 Avatar asked Dec 10 '22 00:12

user3656651


1 Answers

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.

like image 171
Noah Stahl Avatar answered Dec 26 '22 13:12

Noah Stahl