使用POSTMAN客户端的C#web API POST JSON数据在FromBody中始终为NULL

时间:2016-09-28 13:11:54

标签: c# json asp.net-web-api http-post postman

我正在使用包含模型的Web Api -

 public class Assessments
{
    public List<AssessmentResult> Results { get; set; }
}

public class AssessmentResult
{
    public int? AssessorId { get; set; }
    public int? AssessmentId { get; set; }
    public int? CentreId { get; set; }
    public int? BatchId { get; set; }
    public DateTime AssessmentDate { get; set; }

和控制器 -

[HttpPost]
    [ActionName("SetAssessmentResults")]
    public HttpResponseMessage SetAssessmentResults([FromBody] Assessments assessments)
    {
        try
        {

            string sproc = ConfigurationManager.AppSettings["ASSESSORAPP_InsertAssessmentsStaging"];
            //_dt = Helper.ToDataTable(assessments); use this if sending list doesnt work

            Mapper.Initialize(cfg =>
            {
                //cfg.CreateMissingTypeMaps = false;
                cfg.CreateMap<AssessmentResult, AssessmentTable>().IgnoreAllNonExisting();

                //cfg.IgnoreUnmapped();
            });
            List<AssessmentTable> assessmentTable = Mapper.Map<List<AssessmentResult>, List<AssessmentTable>>(assessments.Results);
            Mapper.Configuration.AssertConfigurationIsValid();

            string isSuccess = _db.InsertAssessmentResults(sproc, assessmentTable);
            if (isSuccess.Split('$')[0].ToLower() == "success")
            {
                return Request.CreateResponse(HttpStatusCode.OK, isSuccess);
            }
            else if (isSuccess.Split('$')[0].ToLower() == "duplicate")
            {
                return Request.CreateResponse(HttpStatusCode.OK, isSuccess);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.OK, isSuccess);
            }
        }
        catch (Exception ex)
        {
            return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
        }
    }

客户方面,我正在使用Postman Chrome扩展程序。 我正在发送带有JSON数据的POST请求。但是,我没有在控制器中获取JSON数据(评估对象始终显示NULL值)

assessments object showing NULL value

在客户端,我要求使用PostMan Chrome扩展程序

PostMan Client sending request

这是我的JSON数据:

    {
"assessment":
[{
      "AssessorId": 3,
      "AssessmentId": 123,
      "CentreId": 1,
      "BatchId": null,
      "AssessmentDate": "2016-09-30T00:00:00",

    },{
     "AssessorId": 2,
      "AssessmentId": 123,
      "CentreId": 1,
      "BatchId": null,
      "AssessmentDate": "2016-09-30T00:00:00",
    }]
    }

我还添加了Content-Type:application / json

请帮我解决这个问题。谢谢!

1 个答案:

答案 0 :(得分:1)

在您的班级评估中,您可以为包含AssessmentResult&#34; 结果&#34;但是当您发送帖子时,您正在命名该属性&#34; 评估&#34;它应该是&#34; 结果&#34; (考虑到您有默认的序列化程序配置,例如:序列化时没有降低驼峰的属性名称)

尝试:

= f.select :life_cycle, Plant.life_cycles.keys

另外,作为一种好的做法,您应该在类的构造函数中实例化列表 例如:

  {
     "Results":[{
                 "AssessorId": 3,
                 "AssessmentId": 123,
                 "CentreId": 1,
                 "BatchId": null,
                 "AssessmentDate": "2016-09-30T00:00:00",

               },
               {
                 "AssessorId": 2,
                 "AssessmentId": 123,
                 "CentreId": 1,
                 "BatchId": null,
                 "AssessmentDate": "2016-09-30T00:00:00",
               }]
 }