Blazor - 通过 Http.GetJsonAsync 传递参数

Blazor - passing arguments through Http.GetJsonAsync

在 index.cshtml @functions 中,我能够从数据库中检索数据,同时发送参数 chosenDate,如下所示:

trackList = await Http.GetJsonAsync>("/api/Lopstat/Tracks/" + chosenDate.ToString("yyyy-MM-dd"));

如果我想在请求中发送两个参数怎么办?这可能吗?

如果您想发送两个参数,您可以为这些参数定义一个带有两个字段的 class。此 class 将自动进行 JSON 编码并作为字符串发送

您可以使用签名如下所示的 SendJsonAsync 方法:

  public static async Task<T> SendJsonAsync<T>(this HttpClient httpClient, HttpMethod method, string requestUri, object content)

// And this is how you can use it in your code...
trackList = await Http.SendJsonAsync<Change this to the return type>( HttpMethod.Get, "/api/Lopstat/Tracks", MyObject);