【问题标题】:ASP.NET Core 3.1: Web Api: Same Post Method: Multiple types of Json ObjectsASP.NET Core 3.1:Web Api:相同的发布方法:多种类型的 Json 对象
【发布时间】:2022-02-02 19:13:09
【问题描述】:

我有一个要求,我的 POST 方法(单个端点)需要接受不同类型的 json 字符串并对其进行操作。我尝试了以下方法并收到 404 错误:

using Newtonsoft.Json.Linq;

public async Task<IActionResult> Post([FromBody] string jsonString)
    {
        if (string.IsNullOrEmpty(jsonString)) return BadRequest();
        try
        {
            JObject _jsonObject = JObject.Parse(jsonString);
            string _response = string.Empty;

            if(_jsonObject != null)
            {
                string messageType = _jsonObject.GetValue("objectType").ToString();
                if(messageType.ToLower() == "type1")
                {                        
                    _response = await dataRepository.InsertType1Record(_jsonObject );                        
                } else if (messageType.ToLower() == "type2")
                {
                    _response = await dataRepository.InsertType2Record(_jsonObject );                        
                } else if (messageType.ToLower() == "type3")
                {
                    _response = await dataRepository.InsertType3Record(_jsonObject );
                }
                return Ok(_response);
            }
            return BadRequest();
        }
        catch (Exception e)
        {
            _logger.LogTrace(e, "Exception Error");
            return BadRequest();
        }

    }

当我在 Fiddler 中对此进行测试时,我不断收到 404 错误。我的要求:

Host: localhost:44307
Content-Length: 1563
content-type: text/plain

{
    "objectType": "type1",
    "objectDetails": [
        {
         "field1": "value1",
         "field2": "value2"
        },
       {
         "field1": "value3",
         "field2": "value4"
        }
    ]
}
OR
{
    "objectType": "type2",
    "headerField1": "headerValue1",
    "headerField2": "headerValue2",
    "objectInfo": [
        {
         "field3": "value1",
         "field4": "value2",
         "field5": "value3"
        },
       {
         "field3": "value7",
         "field4": "value8",
         "field5": "value9"
        }
    ]
}

我该如何做到这一点?任何帮助表示赞赏。

编辑:我没有使用 Newtonsoft.Json,而是使用了内置的 JsonSerializer,并按照以下方式进行:

 public async Task<IActionResult> Post([FromBody] System.Text.Json.JsonElement jsonString)
    {            
        try
        { 
            string _jsonString = System.Text.Json.JsonSerializer.Serialize(jsonString);
            string messageType = jsonString.GetProperty("objectType").GetString();

          ......
       }

【问题讨论】:

  • 你是如何路由到这个方法的?您是否缺少 Route 属性?
  • 路由属性为:[Route("api/controller")]
  • 我现在收到 Http 错误:415:不支持的媒体类型错误
  • @Jeppe:感谢您为我指明了正确的方向。

标签: c# asp.net-core asp.net-core-webapi


【解决方案1】:

尝试将内容类型更改为

application/json; charset=utf-8

【讨论】:

  • 我将 Content-Type 更改为 application/json 并将我的 Post 方法更改如下: public async Task Post([FromBody] System.Text.Json.JsonElement eTCCData){string _jsonString = System.Text.Json.JsonSerializer.Serialize(eTCCData); }。有效。谢谢。
  • 我没有使用 Newtonsoft.Json,而是使用了内置的 JsonSerializer。
  • @SoftwareDveloper 很高兴能为您提供帮助。请不要忘记接受答案,因为它很有帮助。
猜你喜欢
  • 1970-01-01
  • 2020-12-03
  • 2021-02-14
  • 1970-01-01
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
  • 2018-05-23
  • 1970-01-01
相关资源
最近更新 更多