對于 HttpPost 函式
public async Task<Result> PostAsync([FromBody]Request request, [FromHeader(Name = "Cs-Auth")] string authKey=null)
{
try{
HttpResponseMessage response = await httpClient.PostAsJsonAsync(_config.ApiUrl, request);
response.EnsureSuccessStatusCode();
string responseText = await response.Content.ReadAsStringAsync();
Result result = JsonSerializer.Deserialize<Result>(responseText);
return ProcessResult(result);
}
catch(Exception e)
{
_logger.LogError(e.ToString());
throw;
}
}
請求類:
public class Request
{
[JsonPropertyName("text")]
public string Text { get; set; }
[JsonPropertyName("user_id")]
public string user_id { get; set; }
[JsonPropertyName("rule_id")]
public string PolicyId { get; set; }
public Policy Policy {get; set;}
}
Json 主體:
{
"text": "sample",
"user_id": "3455643",
...
}
但是我在 PostAsync 中得到的請求看起來像

我期待的是“文本”,而不是“文本”。
我不想將請求類中的屬性名稱“文本”更改為“文本”,我需要做什么來定義序列化/反序列化行為?
uj5u.com熱心網友回復:
一切正常,您的除錯器只是在更改之前顯示資料。在你的除錯器中試試這個
var jsonRequest= JsonConvert.SerializeObject(request);
var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json");
var response = await client.PostAsync(_config.ApiUrl, content);
請求
{
"text": "sample",
"user_id": "3455643",
...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/353659.html
上一篇:ASP.NET異步操作回傳型別
