Send HTTP POST message in ASP.NET Core using HttpClient PostAsJsonAsync

后端 未结 5 1603
灰色年华
灰色年华 2020-11-27 14:20

I want to send dynamic object like

new { x = 1, y = 2 };

as body of HTTP POST message. So I try to write

var client = new H         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 14:45

    You are right that this has long since been implemented in .NET Core.

    At the time of writing (September 2019), the project.json file of NuGet 3.x+ has been superseded by PackageReference (as explained at https://docs.microsoft.com/en-us/nuget/archive/project-json).

    To get access to the *Async methods of the HttpClient class, your .csproj file must be correctly configured.

    Open your .csproj file in a plain text editor, and make sure the first line is

    (as pointed out at https://docs.microsoft.com/en-us/dotnet/core/tools/project-json-to-csproj#the-csproj-format).

    To get access to the *Async methods of the HttpClient class, you also need to have the correct package reference in your .csproj file, like so:

    
        
        
        
    
    

    (See https://docs.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files#adding-a-packagereference. Also: We recommend applications targeting ASP.NET Core 2.1 and later use the Microsoft.AspNetCore.App metapackage, https://docs.microsoft.com/en-us/aspnet/core/fundamentals/metapackage)

    Methods such as PostAsJsonAsync, ReadAsAsync, PutAsJsonAsync and DeleteAsync should now work out of the box. (No using directive needed.)

    Update: The PackageReference tag is no longer needed in .NET Core 3.0.

提交回复
热议问题