我在使用时遇到一个简单但烦人的问题PostAsJsonAsync<T>(..)
扩展方法,我在任何地方都找不到有关解决以下问题的信息。
我的问题是生成的 Json 使用PascaCasing
我需要camelCasing
按实际标准。
这是一个可以重现该问题的简单示例(来源:http://www.codeproject.com/Articles/611176/Calling-ASP-NET-WebAPI-using-HttpClient http://www.codeproject.com/Articles/611176/Calling-ASP-NET-WebAPI-using-HttpClient):
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:56851/");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var user = new Users();
user.FirstName = txtFirst.Text;
user.Company = txtCompany.Text;
user.LastName = txtLas.Text;
user.Email = txtEmail.Text;
user.PhoneNo = txtPhone.Text;
user.Email = txtEmail.Text;
var response = client.PostAsJsonAsync("api/User", user).Result;
if (response.IsSuccessStatusCode)
{
MessageBox.Show("User Added");
txtFirst.Text = "";
txtLas.Text = "";
txtPhone.Text = "";
txtEmail.Text = "";
txtCompany.Text = "";
GetData();
}
else
{
MessageBox.Show("Error Code" +
response.StatusCode + " : Message - " + response.ReasonPhrase);
}
尝试只发送匿名类型。
var user = new
{
firstName = txtFirst.Text,
company = txtCompany.Text,
lastName = txtLas.Text,
email = txtEmail.Text,
phoneNo = txtPhone.Text,
email = txtEmail.Text
};
var response = await client.PostAsJsonAsync("api/User", user);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)