Where is the PostAsJsonAsync method in ASP.NET Core?

后端 未结 14 1749
再見小時候
再見小時候 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:36

    To follow on from the answers above, I have a small addition that was required for me to get it to work.

    Previously I was using a .NET Core 2.1 web app using the PostAsJsonAsync() method, and when I upgraded to .NET Core 3.1 it no longer worked.

    I could not get the above answers to work, and it turned out to be because the text to be posted had to be surrounded by quotes, and any quotes within it had to be escaped. I made the following extension method, which solved my problem:

    public static async Task<HttpResponseMessage> PostJsonAsync(this HttpClient client, string uri, string json)
    {
        //For some reason, not doing this will cause it to fail:
        json = $"\"{json.Replace("\"", "\\\"")}\"";
    
        return await client.PostAsync(uri, new StringContent(json, Encoding.UTF8, "application/json"));
    }
    

    Note that I am using the System.Text.Json.JsonSerializer as opposed to the Newtonsoft version.

    0 讨论(0)
  • 2020-12-29 01:39

    You need to add Nuget package System.Net.Http.Formatting.Extension to your project.

    Or you can use

    client.PostAsync(uri, new StringContent(data, Encoding.UTF8, "application/json"));
    
    0 讨论(0)
  • 2020-12-29 01:41

    That is not part of the ASP.NET Core project. However you can proceed with:

    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://myurl/api");
    
    string json = JsonConvert.SerializeObject(myObj);
    
    request.Content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
    
    HttpClient http = new HttpClient();
    HttpResponseMessage response = await http.SendAsync(request);
    
    if (response.IsSuccessStatusCode)
        {                
        }
    else
        {
        }
    
    0 讨论(0)
  • 2020-12-29 01:42
    using Microsoft.AspNetCore.Blazor.HttpClient
    
    0 讨论(0)
  • 2020-12-29 01:48

    If you are trying to use PostJsonAsync, PutJsonAsync or any other json extension methods in Blazor you need to add a following statement

    using Microsoft.AspNetCore.Components;
    
    0 讨论(0)
  • 2020-12-29 01:49

    I dont deserve any credit for this. Have a look @danroth27 answer in the following link.

    https://github.com/aspnet/Docs/blob/master/aspnetcore/mvc/controllers/testing/sample/TestingControllersSample/tests/TestingControllersSample.Tests/IntegrationTests/HttpClientExtensions.cs

    He uses an extension method. Code as below. (Copied from the above github link). I am using it on .Net Core 2.0.

    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Threading.Tasks;
    using Newtonsoft.Json;
    
    namespace TestingControllersSample.Tests.IntegrationTests
    {
        public static class HttpClientExtensions
        {
            public static Task<HttpResponseMessage> PostAsJsonAsync<T>(
                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 httpClient.PostAsync(url, content);
            }
    
            public static async Task<T> ReadAsJsonAsync<T>(this HttpContent content)
            {
                var dataAsString = await content.ReadAsStringAsync();
                return JsonConvert.DeserializeObject<T>(dataAsString);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题