Not sure if the is the right place to post this issue.
When try to following the Doc for "Integration Testing" example https://github.com/aspnet/Docs/tree/master/aspnetcore/mvc/controllers/testing/sample, for a pure asp.net core project, I got the following error.
CS1061 'HttpClient' does not contain a definition for 'PostAsJsonAsync'
Here are the nudge package was including.
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.3" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="1.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="System.Net.Http" Version="4.3.2" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
I have to add the package below to make it to work.
<PackageReference Include="WinInsider.System.Net.Http.Formatting" Version="1.0.5" />
But this does not seem right because the "TestingControllersSample" does not need to include it.
Is this a packing versioning problem? The "TestingControllersSample" is using "System.Net.Http Version"4.1.1"
Thanks
I'm guessing the sample is missing a using statement.
WinInsider.System.Net.Http.Formatting is not an ASP.NET Core assembly.
We'll investigate, thanks for the report.
@Rick-Anderson I am not sure if it is because of a missing using statement. I believe it is something else, below are the using statements
using Newtonsoft.Json;
using System.Linq;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
using System.Net;
@Rick-Anderson I just read this Where is PostAsJsonAsync method in ASP.NET Core?
on stackoverflow, and realized PostAsJsonAsync is not part of the aspnet.core.
So, I call end up unreference WinInsider.System.Net.Http.Formatting, but calling JsonConvert.SerializeObject(); first then call PostAsync instead.
But I still don't understand how the example https://github.com/aspnet/Docs/blob/master/aspnetcore/mvc/controllers/testing/sample/TestingControllersSample/tests/TestingControllersSample.Tests/IntegrationTests/ApiIdeasControllerTests.cs works by calling PostAsJsonAsync
My problem is solved, but I am not sure if I should close this issue, so please close it if you see fit.
Thanks
@danroth27
I still don't understand how the example https://github.com/aspnet/Docs/blob/master/aspnetcore/mvc/controllers/testing/sample/TestingControllersSample/tests/TestingControllersSample.Tests/IntegrationTests/ApiIdeasControllerTests.cs works by calling PostAsJsonAsync
In this case the sample includes its own implementation of PostAsJsonAsync:
I'm new here, I was pretty confused following that tutorial (Call a Web API From a .NET Client (C#))
I wrote this solution based on @Rick-Anderson comments, I hope this help.
using System.Web.Script.Serialization;
.
.
.
static async Task
{
JavaScriptSerializer jss = new JavaScriptSerializer();
var myContent = jss.Serialize(product);
var httpContent = new StringContent(myContent, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync("api/products", httpContent);
response.EnsureSuccessStatusCode();
//return URI of the create resource.
return response.Headers.Location;
}
Hi!
Just install Microsoft.AspNet.WebApi.Client nuget package
Hi!
Just install Microsoft.AspNet.WebApi.Client nuget package
correct answer.thanks
Most helpful comment
Hi!
Just install Microsoft.AspNet.WebApi.Client nuget package