繁体   English   中英

如何在静态Web API中传递List对象的类

[英]How to pass class of List object in restful web api

我需要在静态Web API中传递类的列表对象。 但是我在Web API中收到null值。 以下是我在Web API中的代码。

    Public class RequestDto
    {
      private string _clientId;
      public string ClientId
      {
          get { return _clientId; }
          set { _clientId= value; }
      }
      private List<DeliveryDocument> _deliveryDocumentInfo;
      public List<DeliveryDocument> DeliveryDocumentInfo
      {
          get { return _deliveryDocumentInfo; }
          set { _deliveryDocumentInfo = value; }
      }   
    }        
    public class DeliveryDocument
    {
       public string DocumentName { get; set; }
       public string DocumentURL { get; set; }
    }

public HttpResponseMessage PostSaveManifest([FromBody] RequestDto manifestRequest)
        {
//here i am receiving null value in list parameter
}

下面的代码用于调用Web API。

var values = new JObject();
values.Add("ClientId", "23824");

var DeliveryDocumentInfo = new List<DeliveryDocument>();
DeliveryDocumentInfo.Add(new DeliveryDocument { DocumentName = "Document Name", DocumentURL = "D:/Documnet/test.png" });
var serOut = JsonConvert.SerializeObject(DeliveryDocumentInfo);
values.Add("DeliveryDocumentInfo", serOut);

HttpContent content = new StringContent(values.ToString(), Encoding.UTF8, "application/json");
                var responsesss = client.PostAsync(Constants.URLValue + "/api/Manifest", content).Result;

在Web API中创建RequestDto对象。 根据OrenHaliva的评论。

var manifest = new RequestDto();
manifest.ClientId = "23824";

var DeliveryDocumentInfo = new List<DeliveryDocument>();

DeliveryDocumentInfo.Add(new DeliveryDocument { DocumentName = "Document Name", DocumentURL = "D:/Documnet/test.png" });

manifest.DeliveryDocumentInfo = DeliveryDocumentInfo;

var serOut = JsonConvert.SerializeObject(manifest);
HttpContent content = new StringContent(serOut, Encoding.UTF8, "application/json");

var responsesss = client.PostAsync(Constants.URLValue + "/api/Manifest", content).Result;

您发送的内容无法序列化为RequestDto类型。 “值”是一个列表,位于您的Dto对象中,但是您的api期望使用dto对象,而不仅仅是其中的列表。

暂无
暂无

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

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