简体   繁体   English

如何在Web API中管理POST请求

[英]How manage a POST request in Web API

As a beginner, I am in trouble with making a REST Web API program. 作为一个初学者,我在编写REST Web API程序时遇到麻烦。 My client application POST data with lab test information of a patient. 我的客户端应用程序POST数据包含患者的实验室测试信息。 Data will be four fields each of Patient and Test information but the number of tests can be varied for each patient. 数据将是患者和测试信息的四个字段,但是每个患者的测试次数可以不同。

Eg: a patient with 1 test, 例如:一位接受过1次检查的患者,

    {
     "patientID": 1121,
     "patientName": "BOB",
     "age": "22",
     "gender": "male",
     "TestID": 10,
     "TubeCode": "GRN",
     "TestName": "HIV",
     "TestCode": "GRN-CHM",
    }

Eg: a patient with 2 test 例如:2次测试的患者

   {
     "patientID": 1122,
     "patientName": "LINDA",
     "age": "26",
     "gender": "Female",
     "TestID": 12,
     "TubeCode": "GRN",
     "TestName": "HIV",
     "TestCode": "GRN-CHM",
     "TestID": 13,
     "TubeCode": "LAV",
     "TestName": "LFT",
     "TestCode": "LAV-CHM",
   }

Eg: a patient with 3 test 例如:3次测试的患者

  {
     "patientID": 1123,
     "patientName": "HARI",
     "age": "29",
     "gender": "male",
     "TestID": 14,
     "TubeCode": "GRN",
     "TestName": "HIV",
     "TestCode": "GRN-CHM",
     "TestID": 15,
     "TubeCode": "LAV",
     "TestName": "LFT",
     "TestCode": "LAV-CHM",
     "TestID": 16,
     "TubeCode": "SPC",
     "TestName": "SPC1",
     "TestCode": "SPC-CHM",
    }

When a client POST all of these examples, API must be able to receive this data. 当客户端发布所有这些示例时,API必须能够接收此数据。

I have tried many ways like adding list object for Test information. 我尝试了很多方法,例如为测试信息添加列表对象。 But couldn't complete because of my poor knowledge. 但是由于我的知识不足而无法完成。

     public void Post([FromBody] Patient_specimenInfo value)
     {           
      // I would like to know how can I manage the FROMBODY content here.
     }

I know how to manage a fixed number of patient and test information. 我知道如何管理固定数量的患者和测试信息。 But here the test information is not fixed for patients... I hope someone can give me the best logic to deal with this kind of situation. 但是,这里的检测信息并不固定给患者...我希望有人可以给我最好的逻辑来处理这种情况。

You can create a list of Tests like 您可以创建一个测试列表,例如

public class Patient
{
  ...
  public List<Test> Tests{get;set;}
  .
  .
}

then from client app send a json like this : 然后从客户端应用程序发送这样的json:

{
 "patientID": 1122,
 "patientName": "LINDA",
 "age": "26",
 "gender": "Female",
 "Tests":
 [
  {
  "TestID": 12,
  "TubeCode": "GRN",
  "TestName": "HIV",
  "TestCode": "GRN-CHM"
  },
  {
  "TestID": 13,
  "TubeCode": "LAV",
  "TestName": "LFT",
  "TestCode": "LAV-CHM"
  }
 ]
}

Provided Json with multiply test isn't well formatted. 为Json提供乘法测试的格式不正确。 You can use Json arrays for the Tests. 您可以将Json数组用于测试。 See this link https://www.w3schools.com/js/js_json_arrays.asp for more details. 有关更多详细信息,请参见此链接https://www.w3schools.com/js/js_json_arrays.asp Eg 例如

{
 "patientID": 1123,
 "patientName": "HARI",
 "age": "29",
 "gender": "male",
 "Tests":[{
   "TestID": 14,
   "TubeCode": "GRN",
   "TestName": "HIV",
   "TestCode": "GRN-CHM"
  },
  {
   "TestID": 15,
   "TubeCode": "LAV",
   "TestName": "LFT",
   "TestCode": "LAV-CHM"
  }]
}

And C# class will look like: C#类将如下所示:

public class Patient_specimenInfo
{
    ...
    public Test[] Tests { get; set;}
    ...
}

public class Test
{
    public int TestID {get; set; }
    public string TubeCode {get; set; }
    public string TestName {get; set; }
    public string TestCode {get; set; }
}

instead of using this 而不是使用这个

public void Post([FromBody] Patient_specimenInfo value)
{           
   // I would like to know how can I manage the FROMBODY content here.
}

please do modify it to 请修改为

public void Post([FromBody] JObject value)
{           
      // here you can Convert the dynamic JObject to a Patient_specimenInfo object accordingly     
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM