【问题标题】:Posting Complex JSON Data in .NET Core WebAPI Project在 .NET Core WebAPI 项目中发布复杂的 JSON 数据
【发布时间】:2022-01-16 07:16:39
【问题描述】:

我需要读取客户端负载发布到我的 API 的 json 数据,如下所示:

{
   "result": {
      "destinationId":"003",
      "message":"msg",
      "sourceEntityId":"1",
      "status":"0"
   }
}

我的HttpPost 代码如下所示:

public ActionResult Post([FromBody] clsTicketinfo ticketInfo)
{
   // Some Code
}

请帮助我在我的HttpPost 方法中读取此 json 数据。以及如何读取客户端连同发布请求一起发送的标头数据。

【问题讨论】:

  • 你也能提供 clsTicketInfo 类吗?
  • 公共类 clsTicketInfo { 字符串影响CIs;字符串影响位置;字符串分配给组; // 许多其他定义了属性的字段 }
  • 这能回答你的问题吗? Post JSON data to Dotnet Core webapi
  • 不,我想是因为我想读取客户端通过邮件发送的数据以及标题
  • 您真的想阅读原始 JSON?为什么?

标签: c# asp.net-mvc .net-core asp.net-web-api


【解决方案1】:

你的clsTicketinfo 类应该是这样的

public class clsTicketInfo 
{ 
    public Result result { get; set;} // <- add this line
    public string affectedCIs {get; set;}
    public string affectedLocations {get; set;}; 
    public string assignedToGroup {get; set;}; 
    // lot of other fields with property defined 
}

您应该创建另一个名称为Result 或任何您想要的类,如下所示:

public class Result
{
    public string destinationId { get; set; }
    public string message { get; set; }
    public string sourceEntityId { get; set; }
    public string status { get; set; }
}

【讨论】:

    猜你喜欢
    • 2019-01-30
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 2020-09-23
    • 1970-01-01
    相关资源
    最近更新 更多