Web API Restful服务ReadAsAsync <type>不返回列表

时间:2015-07-20 18:43:36

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

我试图在类中返回对象列表,但是当我尝试将结果读入容器类时,它会使列表空出来。我对Web API很陌生,所以我不知道这是设计还是我做错了。如果我将响应内容读入字符串,那么列表就在那里。第一张图片显示列表完好无损,第二张图片显示列表不再可用。任何方向将不胜感激! response created null values when readasasync<t>

 using (HttpClient client = new HttpClient())
            {
                string url = Url(String.Format("{0}", id));
                SetupClient(client, url);

                HttpResponseMessage response = client.GetAsync(url).Result;

                if (response.IsSuccessStatusCode)
                {


                    shipment = response.Content.ReadAsAsync<ShipmentViewModel>().Result;

                }

编辑以添加ShipmentViewModel来源

public class ShipmentViewModel
{
    public Guid Id { get; set; }

    public String ShipmentName { get; set; }

    public String Description { get; set; }

    public String Comments { get; set; }

    /// <summary>
    /// Used for UI display purposes only
    /// </summary>

    public int NumberOrdersScanned { get; set; }
    public int NumberOrdersTotal { get; set; }

    public int NumberParcelsScanned { get; set; }
    public int NumberParcelsTotal { get; set; }

    public List<ShipmentEntity> ShipmentEntities { get; set; }

    public ShipmentEntity ShipmentEntity { get; set; }
    public Guid ValidationTypeId { get; set; }
    //        public string ValidationType { get; set; }
    public ValidationType ValidationType { get; set; }
    public List<ValidationType> ValidationTypes { get; set; }
    /// <summary>
    /// Used for UI display purposes only
    /// </summary>
    public List<ShipmentStatus> ShipmentStatuses { get; set; }

    public ShipmentStatus ShipmentStatus { get; set; }

    /// <summary>
    /// Used for UI display purposes only
    /// </summary>
    public List<ShipmentDestinationCountry> DestinationCountries { get; set; }

    public ShipmentDestinationCountry DestinationCountry { get; set; }

    private List<ShipmentTrackingNumber> _TrackingNumbers;
    public List<ShipmentTrackingNumber> TrackingNumbers
    {
        get
        {
            return _TrackingNumbers ?? (_TrackingNumbers = new List<ShipmentTrackingNumber>());
        }
        set
        {
            _TrackingNumbers = value;
        }
    }

    public bool RequiresValidation { get; set; }
    private List<ShipmentValidationViewModel> _Validations { get; set; }

    public List<ShipmentValidationViewModel> Validations
    {
        get
        {
            return _Validations ?? (_Validations = new List<ShipmentValidationViewModel>());
        }
        set
        {
            _Validations = value;
        }
    }

    public Guid EdiShipmentInfoId { get; set; }
    public EdiShipmentInfoViewModel EdiShipmentInfo { get; set; }

    public ShipmentEvents Events { get; set; }
}

这是EdiShipmentInfoViewModel

 public class EdiShipmentInfoViewModel
{
    //        private string _packagingCode;
    public Guid Id { get; set; }
    public Guid ShipmentId { get; set; }
    public Guid WarehouseId { get; set; }
    public virtual InternalLookupModel Warehouse { get; set; }
    public DateTime? ShippingDate { get; set; }
    public DateTime? DeliveryDate { get; set; }
    public string EquipmentNumber { get; set; }

    public string ShippingWeight { get; set; }

    public string PackagingCode { get; set; }

    public string CarrierCommonName { get; set; }
    public Guid ScacCodeId { get; set; }
    public virtual InternalLookupModel ScacCode { get; set; }
    public Guid PackagingFormId { get; set; }
    public virtual InternalLookupModel PackagingForm { get; set; }
    public Guid PackagingMaterialId { get; set; }
    public virtual InternalLookupModel PackagingMaterial { get; set; }

    public string MasterBillLading { get; set; }
    public string BillLading { get; set; }
    public string LadingDescription { get; set; }
    public string FobShippingPoint { get; set; }
    public string FobDestination { get; set; }
    public Guid TransPayMethodId { get; set; }
    public virtual InternalLookupModel TransPayMethod { get; set; }
    public Guid TransTypeId { get; set; }
    public virtual InternalLookupModel TransType { get; set; }
    public Guid ServiceLevelCodeId { get; set; }
    public virtual InternalLookupModel ServiceLevelCode { get; set; }
    public Guid UnitOfMeasureId { get; set; }
    public virtual InternalLookupModel UnitOfMeasure { get; set; }


    private List<InternalLookupModel> _WarehouseList;
    public List<InternalLookupModel> WarehouseList
    {
        get { return _WarehouseList ?? new List<InternalLookupModel>(); }
        set { this._WarehouseList = value; }
    }

    private List<InternalLookupModel> _ScacCodeList;

    public List<InternalLookupModel> ScacCodeList
    {
        get { return _ScacCodeList ?? new List<InternalLookupModel>(); }
        set { this._ScacCodeList = value; }
    }
    private List<InternalLookupModel> _PackagingFormList;
    public List<InternalLookupModel> PackagingFormList
    {
        get { return _PackagingFormList ?? new List<InternalLookupModel>(); }
        set { this._PackagingFormList = value; }
    }

    private List<InternalLookupModel> _PackagingMaterialList;
    public List<InternalLookupModel> PackagingMaterialList
    {
        get { return _PackagingMaterialList ?? new List<InternalLookupModel>(); }
        set { this._PackagingMaterialList = value; }
    }

    private List<InternalLookupModel> _TransPayMethodList;
    public List<InternalLookupModel> TransPayMethodList
    {
        get { return _TransPayMethodList ?? new List<InternalLookupModel>(); }
        set { this._TransPayMethodList = value; }
    }

    private List<InternalLookupModel> _TransTypeList;
    public List<InternalLookupModel> TransTypeList
    {
        get { return _TransTypeList ?? new List<InternalLookupModel>(); }
        set { this._TransTypeList = value; }
    }

    private List<InternalLookupModel> _ServiceLevelCodeList;

    public List<InternalLookupModel> ServiceLevelCodeList
    {
        get { return _ServiceLevelCodeList ?? new List<InternalLookupModel>(); }
        set { this._ServiceLevelCodeList = value; }
    }

    private List<InternalLookupModel> _UnitOfMeasureList;

    public List<InternalLookupModel> UnitOfMeasureList
    {
        get { return _UnitOfMeasureList ?? new List<InternalLookupModel>(); }
        set { this._UnitOfMeasureList = value; }
    }
}

1 个答案:

答案 0 :(得分:2)

我认为@BrianBall是正确的。重写您的类,以便列表属性是简单的getter / setter并初始化构造函数中的列表。

public class ShipmentViewModel
{
  public ShipmentViewModel()
  {
    TrackingNumbers = new List<ShipmentTrackingNumber>();
  }

  public List<ShipmentTrackingNumber> TrackingNumbers { get; set; }
  // and so on
}