postasync c# returns null
C#
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);
}
Also in C#:
- Title
- how to wait in c#
- Category
- C#
- Title
- how to see if they are aholding down a key unity
- Category
- C#
- Title
- c# sort list
- Category
- C#
- Title
- c# window instantly close
- Category
- C#
- Title
- convert string array to int C#
- Category
- C#
- Title
- hot to move pobject unity
- Category
- C#
- Title
- 2D follow ia unity 2D with agrorange
- Category
- C#
- Title
- perlin noise unity
- Category
- C#
- Title
- autoresetevent
- Category
- C#
- Title
- center an image horizontally and vertically
- Category
- C#
- Title
- asp.net core mvc not triggering client side validation
- Category
- C#
- Title
- unity cast float to int
- Category
- C#
- Title
- c# making a folder
- Category
- C#
- Title
- trello
- Category
- C#
- Title
- C# move form without border
- Category
- C#
- Title
- c# getter setter
- Category
- C#
- Title
- open file in explorer c#
- Category
- C#
- Title
- c# remove specific character from string
- Category
- C#
- Title
- c# read a webpage data
- Category
- C#
- Title
- c# make request to rest api
- Category
- C#
- Title
- how to change the title of the console in c#
- Category
- C#
- Title
- unity 2d platformer movement script c#
- Category
- C#
- Title
- write string multiple times c#
- Category
- C#
- Title
- c# print
- Category
- C#
- Title
- c# if statement
- Category
- C#
- Title
- c sharp string replace
- Category
- C#
- Title
- unity particle system color
- Category
- C#
- Title
- c# get current directory xamarin
- Category
- C#
- Title
- unity how to load up a scene
- Category
- C#
- Title
- json stringify c#
- Category
- C#
- Title
- wpf c# select folder path
- Category
- C#
- Title
- how to reduce garbage collection c#
- Category
- C#
- Title
- c# find element by condition
- Category
- C#
- Title
- how to clear console through script unity
- Category
- C#
- Title
- c# abstract class
- Category
- C#
- Title
- letter at index of string c#
- Category
- C#
- Title
- declare string array c# without size
- Category
- C#
- Title
- c# unity rotate first person controller script
- Category
- C#
- Title
- unity simple fps controller
- Category
- C#
- Title
- object escape player unity
- Category
- C#
- Title
- unity how to get y value
- Category
- C#
- Title
- unity set object scale
- Category
- C#
- Title
- C# function return datareader
- Category
- C#
- Title
- vb.net drag window without titlebar
- Category
- C#
- Title
- How to get number of months between 2 dates c#
- Category
- C#
- Title
- select a whole row out of a 2d array C#
- Category
- C#
- Title
- c# mailmessage set sender name
- Category
- C#
- Title
- how to change an int value c#
- Category
- C#
- Title
- c sharp comments
- Category
- C#
- Title
- c# how to exit program
- Category
- C#
- Title
- c# read all text from a file
- Category
- C#
- Title
- array to list C
- Category
- C#
- Title
- c# ienumerable to list
- Category
- C#
- Title
- unity how to check object position
- Category
- C#
- Title
- c# integer to bit string
- Category
- C#
- Title
- asp.net core task iactionresult
- Category
- C#
- Title
- c# string remove special characters
- Category
- C#
- Title
- c# int
- Category
- C#
- Title
- how to make a float in C++
- Category
- C#
- Title
- sending data photon c#
- Category
- C#
- Title
- how to move towards an object unity
- Category
- C#
- Title
- how to change player cursor c# script unity
- Category
- C#
- Title
- c# double question mark
- Category
- C#
- Title
- C# sprint key
- Category
- C#
- Title
- where keyword in c#
- Category
- C#
- Title
- blazor wasm routable page in separate project
- Category
- C#
- Title
- kill child C#
- Category
- C#
- Title
- check if multiple variables are null c#
- Category
- C#
- Title
- unity button interactable
- Category
- C#
- Title
- c# get index of item in list
- Category
- C#
- Title
- max of array C#
- Category
- C#
- Title
- xamarin hide back button
- Category
- C#
- Title
- get random value from list c#
- Category
- C#
- Title
- How do i destroy a prefab without the error?
- Category
- C#
- Title
- nullable unique constraint ef
- Category
- C#
- Title
- response redirect new tab
- Category
- C#
- Title
- Assets/Scripts/Snake.cs(187,10): error CS0029: Cannot implicitly convert type `UnityEngine.Vector2Int' to `System.Collections.Generic.List<UnityEngine.Vector2Int>'
- Category
- C#
- Title
- new command - latex
- Category
- C#
- Title
- c# round to closest multiple
- Category
- C#
- Title
- ASP select box all states
- Category
- C#
- Title
- unity timer with miliseconds
- Category
- C#
- Title
- c# checksum
- Category
- C#
- Title
- c# how to sort a list
- Category
- C#
- Title
- what does thismean incsharp public static void Main
- Category
- C#
- Title
- unity remove gameobject
- Category
- C#
- Title
- convert array to list Unity C#
- Category
- C#
- Title
- c# read file stream
- Category
- C#
- Title
- abstract class c#
- Category
- C#
- Title
- how to parse a string to an integer c#
- Category
- C#
- Title
- unity3d invector expand fsm controller
- Category
- C#
- Title
- unity rotate object c#
- Category
- C#
- Title
- how to convert object in string JSON c#
- Category
- C#
- Title
- c# download file
- Category
- C#
- Title
- get list length c#
- Category
- C#
- Title
- c# get value of object in enum
- Category
- C#
- Title
- how to flip selection in aseprite
- Category
- C#
- Title
- C# graph api upload file one drive
- Category
- C#
- Title
- c# enum default
- Category
- C#
- Title
- c# anonymous class
- Category
- C#
- Title
- how to get array of children transform
- Category
- C#