【发布时间】:2021-12-19 21:59:02
【问题描述】:
对于 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 中得到的请求看起来像
我期待的是“文本”,而不是“文本”。
我不想将请求类中的属性名称“文本”更改为“文本”,我需要做什么来定义序列化/反序列化行为?
【问题讨论】:
标签: c# json asp.net-mvc http-post