如何在静态Web API中传递List对象的类 [英] How to pass class of List object in restful web api

查看:59
本文介绍了如何在静态Web API中传递List对象的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to pass list object of class in restful web api. But i am receiving null value in web api. Below is my code in 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
}

以下用于调用网络api的代码.

Below code for calling 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 注释.

Create object of RequestDto in Web API. As per OrenHaliva comments.

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;

这篇关于如何在静态Web API中传递List对象的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆