首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HttpClient - PostAsJsonAsync

HttpClient - PostAsJsonAsync
EN

Stack Overflow用户
提问于 2015-11-13 03:56:30
回答 1查看 8.5K关注 0票数 2

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

我的问题是,生成的Json使用PascaCasing,而根据实际标准,我需要camelCasing

下面是一个可以重现这个问题的简单示例(来源:http://www.codeproject.com/Articles/611176/Calling-ASP-NET-WebAPI-using-HttpClient):

代码语言:javascript
复制
        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);
        }
EN

回答 1

Stack Overflow用户

发布于 2015-11-13 05:05:49

尝试只发送匿名类型。

代码语言:javascript
复制
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;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33680203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档