I have the following endpoint
[HttpPost]
[DisableRequestSizeLimit]
[RequestFormLimits(KeyLengthLimit = int.MaxValue)]
public IActionResult PostData([FromForm]Data data)
The Data class looks like this
public class Data
{
public string A { get; set; }
public string B { get; set; }
}
I am calling this endpoint in this way
var url = ...;
var client = new HttpClient();
var data = new
{
a = "Foo",
b = "Bar"
};
var result = await client.PostAsJsonAsync(url, data);
But the data parameter in the PostData method always is null. Any ideas what I am doing wrong?
outparameter so what doescomes backmean?resultwon't be null either, it will always contain the responseFromForm? but posting as JSON. Not form was wentFromFormthat - forms don't use JSON - JSON was invented at least a decade later. Use a FormUrlEncodedContent instace withPostAsync. All it needs as input is anything that implementsIEnumerable<System.Collections.Generic.KeyValuePair<string,string>>like aDictionary<string,string>PostDatamethod isn't used with forms, you could just remove[FromForm]