2013-12-12 40 views
1

我在嘗試通過發佈請求調用我的web api時遇到問題,返回空數組。Request.CreateResponse將空白數據返回給郵遞員

我的方法是:

// POST: Api/v1/transaction/ 
    [HttpPost] 
    public HttpResponseMessage Post(string user) 
    { 

     var userId = new Guid(user); 

     var transactions = new Collection<TransactionDataTransferObject>(); 

     try 
     { 
      var seller = _databaseContext.Sellers.Single(s => s.Id == userId); 
      var sellerMedias = _databaseContext.Medias.Where(m => m.TakenBy.Id == seller.Id); 

      foreach (var sellerMedia in sellerMedias) 
      { 

       var allLogsForMedia = _databaseContext.Logs.Where(l => l.ObjectReferenceId == sellerMedia.Id); 

       foreach (var logMedia in allLogsForMedia) 
       { 
        var transaction = new TransactionDataTransferObject 
         { 
          Date = logMedia.DateTimeInUtc, 
          Amount = sellerMedia.PriceInSek, 
          MediaName = sellerMedia.FileName, 
          UserName = seller.FirstName + " " + seller.LastName 
         }; 

        transactions.Add(transaction); 
       } 
      } 
     } 

     catch (Exception exception) 
     { 

      return Request.CreateErrorResponse(HttpStatusCode.NotFound, exception); 
     } 

     return Request.CreateResponse(HttpStatusCode.OK, transactions); 
    } 

當調試交易變量,我看到集合中的兩個對象。 我對郵遞員的回覆是:

[ 
    {}, 
    {} 
] 

我做錯了什麼?我的數據在哪裏發送?

回答

1

好吧,經過幾個小時的表頭抨擊我發現我用 [DataContract]作爲過濾ViewModel,TransactionDataTransferObject。

像這樣:

[DataContract] 
public class TransactionDataTransferObject 
{ 
    [Display(Name = "Date")] 
    public DateTime Date { get; set; } 

    public string MediaName { get; set; } 

    public Guid MediaId { get; set; } 

    public string UserName { get; set; } 

    public Guid UserId { get; set; } 

    [Display(Name = "Description")] 
    public string Discriminator { get; set; } 

    [Display(Name = "Amount")] 
    public decimal Amount { get; set; }  

} 

這是錯誤在這種情況下...

感謝您的閱讀!

+0

這幫了我。在我的情況下,我需要添加'[DataMember]'到我想序列化的屬性。 –