Where Is Httpcontent.Readasasync

Where is HttpContent.ReadAsAsync?

It looks like it is an extension method (in System.Net.Http.Formatting):

HttpContentExtensions Class

Update:

PM> install-package Microsoft.AspNet.WebApi.Client

According to the System.Net.Http.Formatting NuGet package page, the System.Net.Http.Formatting package is now legacy and can instead be found in the Microsoft.AspNet.WebApi.Client package available on NuGet here.

Has HttpContent.ReadAsAsyncT method been superceded in .NET Core?

I can't tell from the code if it ever was an instance method but it probably was.

The links you included alternate between .net 4.x and .net core, it's not clear if you are aware of this. Labelling them with dates suggests a linear progression but we have a fork in the road.

And that is all, it was 'demoted' to residing in an additional package because it will be used less. In .net core we now have similar extensionmethods acting on HttpClient directly.


In order to use this with .net core 3.x you may have to add the System.Net.Http.Json nuget package. The extensions only work with System.Text.Json, for Newtonsoft you will have to use the traditional code patterns.

How to use “ReadAsAsync” in .core

i did this before here
You will have to install Newtonsoft.Json nuget package

CuttlyApiKey is your api key and SelectedCustomText is custom name for your link, you can set string.empty if you dont want to set custom name

public async Task<string> CuttlyShorten(string longUrl)
{

try
{
string url = string.Format("https://cutt.ly/api/api.php?key={0}&short={1}&name={2}", CuttlyApiKey, HttpUtility.UrlEncode(longUrl), SelectedCustomText);

using (HttpClient client = new HttpClient())
using (HttpResponseMessage response = await client.GetAsync(url))
using (HttpContent content = response.Content)
{
dynamic root = JsonConvert.DeserializeObject<dynamic>(response.Content.ReadAsStringAsync().Result);

string statusCode = root.url.status;
if (statusCode.Equals("7"))
{
string link = root.url.shortLink;

return link;
}
else
{
string error = root.status;

}

}
}
catch (Exception ex)
{

}

return "error";
}

usage:

var shortUrl = await CuttlyShorten(url);

System.Net.Http.HttpContent' does not contain a definition for 'ReadAsAsync' and no extension method

After a long struggle, I found the solution.

Solution: Add a reference to System.Net.Http.Formatting.dll. This assembly is also available in the C:\Program Files\Microsoft ASP.NET\ASP.NET MVC 4\Assemblies folder.

The method ReadAsAsync is an extension method declared in the class HttpContentExtensions, which is in the namespace System.Net.Http in the library System.Net.Http.Formatting.

Reflector came to rescue!

HttpContent ReadAsAsync map data coming from api c#

JSON.NET that is used by default for deserialization supports JsonProperty attribute for adjusting JSON field name:

public class Report 
{
[JsonProperty("pK_LibraryDocument")]
public int id { get; set; }

public string fileName { get; set; }
public List<string> AvailableForModules { get; set; }
}

Migrating .NET Core 2 to .NET Core 3: HttpContent does not contain a definition for ReadAsAsync

ReadAsAsync is a .NET Standard extension that's actually shared between ASP.NET Core and ASP.NET Web Api (via a NuGet library). However, it uses JSON.NET to do the deserialization, and as of .NET Core 3.0, ASP.NET Core now uses System.Text.Json instead. As such, this library (and the extension it contains) is not included in the .NET Core 3.0 framework because doing so would require including the JSON.NET library in addition to System.Text.Json.

While you can manually add the Microsoft.AspNet.WebApi.Client (and Newtonsoft.Json along with it), you should just move on without it. It doesn't save you much anyways, as you can accomplish the same via just:

await JsonSerializer.DeserializeAsync<MyType>(await response.Content.ReadAsStreamAsync());

If you like, you can add your own extension to HttpContent to wrap this up in a ReadAsAsync method:

public static class HttpContentExtensions
{
public static async Task<T> ReadAsAsync<T>(this HttpContent content) =>
await JsonSerializer.DeserializeAsync<T>(await content.ReadAsStreamAsync());
}

HttpContent.ReadAsAsync method not returning correct value from a Web API call

I realized that this happens because I am returning HttpStatusCode.Created for the initial action (user creation), however return HttpStatusCode.NoContent for subsequent actions on the same user.

if (result == RegisterUserResult.AlreadyExists)
{
statusCode = HttpStatusCode.NoContent;
}

While HttpStatusCode.NoContent is a successful status code, it will prevent providing a return value in Request.CreateResponse method by returning the default value of your intended return type. Meaning

Request.CreateResponse(statusCode, "True"); // returns null on client-side
Request.CreateResponse(statusCode, true); // returns false on client-side

Other success codes such as HttpStatusCode.OK or HttpStatusCode.Created will make sure that the intended value will be returned.



Related Topics



Leave a reply



Submit