简体   繁体   English

从 Blazor 应用程序中的 WEB API 返回数据

[英]Returning data from a WEB API in a Blazor App

Consider the following two pieces of code.考虑以下两段代码。 Both return data to a Web API Get call.两者都将数据返回到 Web API 调用。 Both return a list of items.两者都返回一个项目列表。 Both work.两者都有效。 The first one was taken from Visual Studio starter Blazor Wasm App.第一个取自 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. tblTitles 是远程数据库中的一个表,通过_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 .我相信您会注意到不同的可用 controller 返回类型 From that docs page:从该文档页面:

ASP.NET Core offers the following options for web API controller action return types: ASP.NET 内核为 web API Z594C103F2C6E04C3D18AB059F031E0 类型提供以下选项返回:C

  • Specific type具体类型
  • IActionResult
  • ActionResult<T>

The page offers considerations of when to use each.该页面提供了何时使用每种方法的注意事项。

Both are the same.两者都是一样的。 By default, asp .net treats action methods as HttpGet if no attribute is specified.默认情况下,如果没有指定属性,asp .net 会将操作方法视为 HttpGet。 Now both return a list, but you should consider as well to return a specific status code as part of your api.现在两者都返回一个列表,但您也应该考虑返回一个特定的状态代码作为 api 的一部分。 In that way the client application can understand what happened with the request通过这种方式,客户端应用程序可以了解请求发生了什么

IMHO, I will go with this approach for your api恕我直言,我将为您的 api 使用这种方法 go

[HttpGet]
 public async Task<IActionResult> Get()
 {
    var titles = await _dataContext.tblTitles.ToListAsync();
    return Ok(titles);
 }

You can return a specific type instead of an IActionResult which is the generic representation but you are only going to see advantage of that when you need to generate the documentation for you api using the open api standard.您可以返回特定类型而不是通用表示形式的 IActionResult,但只有当您需要使用开放的 api 标准为您生成 api 文档时,您才会看到它的优势。 Packages like swashbuckle inspect via reflection the return type to generate the proper output model on the documentation.像 swashbuckle 这样的包通过反射检查返回类型以在文档上生成正确的 output model。 Hope this helps希望这可以帮助

There are 2 differences between your snippets, they should be considered separately.您的片段之间有 2 个差异,应单独考虑。

1. Sync or Async 1.同步或异步

Most actions do some I/O and the async/await patterns is much preferred for handling that, it will allow your server to handle much more concurrent requests.大多数操作都会执行一些 I/O,并且异步/等待模式更适合处理它,它将允许您的服务器处理更多的并发请求。

2. Plain data or IActionResult 2. 纯数据或 IActionResult

Returning data directly is asking ASP.NET to wrap it in a response for you.直接返回数据是要求 ASP.NET 将其包装为您的响应。 That is easy but as soon as you try to add validations it becomes problematic.这很容易,但是一旦您尝试添加验证,就会出现问题。 Any error will be returned as status 500 (Internal server error), a well designed API should be able to return 400 (Bad request) or 404 (Not Found) when applicable.任何错误都将返回状态 500(内部服务器错误),设计良好的 API 应该能够在适用时返回 400(错误请求)或 404(未找到)。

So, in conclusion, async Task<IActionResult> or async Task<ActionResult<T>> are the best patterns for a Controller Action.因此,总而言之, async Task<IActionResult>async Task<ActionResult<T>>是 Controller Action 的最佳模式。


The first one was taken from Visual Studio starter Blazor Wasm App.第一个取自 Visual Studio starter Blazor Wasm App。

No, it wasn't.不,不是。 The demo controller generates some weatherdata but does not do any I/O.演示 controller 生成一些天气数据,但不执行任何 I/O。

Which makes the chosen short form acceptable, but consider it to be of 'demo' quality level.这使得选择的简短形式可以接受,但认为它具有“演示”质量水平。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM