I don't know how send values strings or models to a HttpPost-action
I want to send a value to a HttpPost-action in a API. He reach the HttpPost-action. But the value of parameter name is NULL. What do I wrong?
By example the value of "name" = "Netherlands".
public async Task<long> UpdateCountry(string name)
{
string url = $"{myApi}/Address/UpdateCountry";
var model = JsonConvert.SerializeObject(new KeyValuePair<string, string>("name", name));
long id = await Post(url, model);
return id;
}
than the process starts in the BaseClass... in the function Post.
protected async Task<dynamic> Post(string url, string data)
{
var client = new HttpClient();
var httpContent = new StringContent(data);
HttpResponseMessage responseMessage = await client.PostAsync(url, httpContent);
var result = await responseMessage.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<dynamic>(result);
}
And in the API the value of parameter name is NULL.
[HttpPost("UpdateCountry")]
public async Task<long> UpdateCountry(string name)
{
var countryId = _countryService.GetIdByName(name);
if (countryId == null)
{
var dto = new CountryDto() { Name = name };
....
countryId = await _countryService.Insert(dto);
}
else
{
dto.Name = name;
countryId = await _countryService.Update(dto);
}
return countryId.Value;
}