2014-03-28 47 views
0

我有一個MVC項目發佈到WebAPI。使用重音字符(é)時,WebAPI [FromBody]的對象爲空。當沒有重音字符時它會正確填充。 MVC對象是正確的,所以我不知道爲什麼會有從JSON到我的API對象的轉換問題。 JSON由Newtonsoft.Json.JsonConvert.SerializeObject版本6.0.1生成。WebApi FromBody對象null當一個字段包含一個變音符

using (var client = new HttpClient()) 
{ 
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _authorizationString); 
    string json = Newtonsoft.Json.JsonConvert.SerializeObject(model); 

    var response = await client.PostAsync(_url, new StringContent(json, System.Text.Encoding.Default, "application/json")); 
    string content = await response.Content.ReadAsStringAsync(); 
    if (response.IsSuccessStatusCode) 
    { 
     model = JsonConvert.DeserializeObject<Model>(content); 
     TempData["Model"] = model; 
     return RedirectToAction("Confirmation"); 
    } 

}

//API 
[HttpPost] 
public HttpResponseMessage ApiMethod([FromBody] ApiObject obj) 
{ 
    //obj is null here, but only when there is an accent in one of the obj string properties. 
    ... 
} 

我剛剛發現一兩件事。我可以用Fiddler成功發佈到API。我從這一行復制了JSON,字符串json = Newtonsoft.Json.JsonConvert.SerializeObject(model);

因此,也許PostAsync有錯誤?

+0

顯示您的控制器操作的代碼。 –

回答

0

我在PostAsync中切換了編碼。 Unicode和ASCII都有效。 UTF32沒有。

相關問題