gpt4 book ai didi

c# - HttpClient-PostAsJsonAsync

转载 作者:太空宇宙 更新时间:2023-11-03 12:56:22 26 4
gpt4 key购买 nike

我在使用 PostAsJsonAsync<T>(..) 时遇到了一个简单但烦人的问题扩展方法,我在任何地方都找不到有关解决以下问题的信息。

我的问题是生成的 Json 使用 PascaCasing我需要 camelCasing以实际标准为准。

这是一个可以重现问题的简单示例(来源: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 = client.PostAsJsonAsync("api/User", user).Result;

关于c# - HttpClient-PostAsJsonAsync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33680203/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com