"postasync c# returns null" Code Answer's

You're definitely familiar with the best coding language C# that developers use to develop their projects and they get all their queries like "postasync c# returns null" answered properly. Developers are finding an appropriate answer about postasync c# returns null related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like postasync c# returns null. Enter your desired code related query in the search bar and get every piece of information about C# code related question on postasync c# returns null. 

postasync c# returns null

By Innocent IbexInnocent Ibex on May 09, 2020
As indicated in the comments the model was not being converted to JSON when you called model.ToString. You eventually figured out that you can use Json.Net to serialize the model to JSON with JsonConvert.SerializeObject(model). This will work for serializing the model to JSON.
You could go one step further and create an extension method to perform that functionality for you
public class JSONStringExtension {
	public static string ToJsonString(this object model) {
      if(model is string) throw new ArgumentException("mode should not be a string");
      return JsonConvert.SerializeObject(model);
	}
}
This will now allow you to call the method on your model and covert it to JSON in your code.
var baseUri = new Uri("http://localhost:5001/"):
_httpClient.BaseAdress = baseUri;
var data = new StringContent(
	content: model.ToJsonString(), //<--Extension method here
	encoding: Encoding.UTF8,
	mediaType: "application/json"
);
var response = await _httpClient.PostAsync("api/product", data);
The PostAsJsonAsync extension method that is frequently used basically performs the same thing you eventually realized by abstracting the JSON serialization step for you.
Internally it is calling the same PostAsync method.
which would look something a little like this
public static Task<HttpResponseMessage> PostAsJsonAsync(this HttpClient httpClient, string url, object content) {
	var json = JsonConvert.SerializeObject(content)
	var data = new StringContent(
    	content: json,
    	encoding: Encoding.UTF8,
    	mediaType: "application/json"
    );
    return httpClient.PostAsync(url, data);
}

Source: csharp.developreference.com

Add Comment

0

All those coders who are working on the C# based application and are stuck on postasync c# returns null can get a collection of related answers to their query. Programmers need to enter their query on postasync c# returns null related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about postasync c# returns null for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of postasync c# returns null while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "postasync c# returns null". Visit this developer's friendly online web community, CodeProZone, and get your queries like postasync c# returns null resolved professionally and stay updated to the latest C# updates. 

C# answers related to "postasync c# returns null"

View All C# queries

C# queries related to "postasync c# returns null"

Browse Other Code Languages

CodeProZone