Я отправляю словарь со следующим вызовом:
public async Task<string> Test(string val1, string val2)
{
string res = null;
using (HttpClient http = new HttpClient())
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("value1", val1);
dic.Add("value2", val2);
string data = JsonHelper.Serialize<Dictionary<string, string>>(dic);
using (StringContent content = new StringContent(data, Encoding.UTF8, "application/json"))
{
Uri uri = new Uri("http://www.whatever.com/api/test");
using (HttpResponseMessage response = await http.PostAsync(uri, content))
{
res = await response.Content.ReadAsAsync<string>();
}
}
}
return res;
}
и вижу, что на принимающей стороне словарь всегда десериализуется как пустой.
[HttpPost]
public IHttpActionResult Test(Dictionary<string, string> req)
{
return Ok();
}
Что я делаю не так?
Если я пытаюсь получить тело запроса (string body = await Request.Content.ReadAsStringAsync();
) и десериализовать его вручную с помощью DataContractJsonSerializer, я получаю ожидаемый словарь.