1
當我使用PostAsJsonAsync<T>(..)
擴展方法時遇到了一個簡單但令人討厭的問題,我找不到有關在任何地方解決以下問題的信息。HttpClient - PostAsJsonAsync
我的問題是,生成的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);
}
在我來說,我送一個匿名類型,但是這絕對不是解決了我,因爲我有複雜的數據結構有多個級別,並在每個請求重新定義不希望我的模型。 – LostBalloon
@LostBalloon gotcha ..也許這將有助於http://www.newtonsoft.com/json/help/html/contractresolver.htm或http://james.newtonking.com/archive/2013/05/08/json- net-5-0-release-5-defaultsettings-and-extension-data – JamieD77
是的,我最終爲此使用了Newtonsoft,但我無法相信他們花時間爲'PostAsJsonAsync(..)'編寫代碼它是完全無用的。有沒有什麼我們都失蹤了? –
LostBalloon