Where is the PostAsJsonAsync method in ASP.NET Core?

后端 未结 14 1850
再見小時候
再見小時候 2020-12-29 00:51

I was looking for the PostAsJsonAsync() extension method in ASP.NET Core. Based on this article, it\'s available in the Microsoft.AspNet.WebApi.Client

14条回答
  •  孤独总比滥情好
    2020-12-29 01:30

    make the extension method truly async:

    public static async Task PostAsJsonAsync(
        this HttpClient httpClient, string url, T data)
    {
        var dataAsString = JsonConvert.SerializeObject(data);
        var content = new StringContent(dataAsString);
        content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        return await httpClient.PostAsync(url, content);
    }
    

提交回复
热议问题