2015-10-05 161 views
0

我剛剛開始一些R & D集成了一個調用WebApi的MVC應用程序,我正在編寫這兩個應用程序。 WebApi最終會被不同的應用程序調用,所以我想在這裏有業務邏輯。如何從PostAsync獲取模型數據?

我已經創建了下面這個簡單的方法我API的服務器上:

[HttpPost] 
public HttpResponseMessage Get(ShipmentGetCarrierModel view) 
{ 
    var response = Request.CreateResponse<ShipmentGetCarrierModel>(HttpStatusCode.OK, view); 
    return response; 

    //return new HttpResponseMessage(HttpStatusCode.OK); 
} 

我從我的MVC應用程序中調用此方法如下:

public ActionResult GetOrderNumber2(int orderNumber) 
{ 
    using (var apiServer = new HttpClient()) 
    { 
     apiServer.BaseAddress = new Uri("http://localhost:52126"); 
     apiServer.DefaultRequestHeaders.Accept.Clear(); 
     apiServer.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
     ShipmentGetCarrierModel model = new ShipmentGetCarrierModel(); 
     model.nOrderNumber = 100; 

     // New code: 
     HttpResponseMessage response = 
      apiServer.PostAsync("api/PC/Get", 
      model, 
     new JsonMediaTypeFormatter()).Result; 


     if (response.IsSuccessStatusCode) 
     { 
      return Content(response.Content.ToString()); 
     } 
    } 

    return Content("NOT " + orderNumber.ToString()); 
} 

我的模型,它是兩個應用程序相同是:

public class ShipmentGetCarrierModel 
{ 
    public int nID { get; set; } 
    public int nOrderNumber { get; set; } 
    public string cConsigneeName { get; set; } 
    public string cCarrierCode { get; set; } 
    public string cConsigneeAddress1 { get; set; } 
    public string cConsigneeAddress2 { get; set; } 
    public string cConsigneeCity { get; set; } 
    public string cConsigneeProvince { get; set; } 
    public string cConsigneePostalCode { get; set; } 
    public string cConsigneeTelephone { get; set; } 
    public string cConsigneeContact { get; set; } 
    public string cConsigneeCountry { get; set; } 
    public Boolean? lIsPharmilink { get; set; } 
} 

我得到的響應代碼,但我看不到模型dat一個在WebAPI創建的調試器中返回的結果。

諷刺的是,當我使用招分析了電話,我看到我的模型與他們的空值。

Fiddler Image

顯然,我在這裏失去了一些東西。 TIA 馬克

+0

「我看不到模型數據」您期望看到它但它不在那裏? – usr

+0

也許你應該考慮返回一個JsonResult例如: 返回JSON(視圖,JsonRequestBehavior.AllowGet); –

+0

我剛剛添加了上面的模型代碼。 – Mark

回答

1

我終於發現我在做什麼錯誤的;我在撥打HttpClient後錯過了兩個步驟

public ActionResult GetOrderNumber2(int orderNumber) 
{ 
    using (var apiServer = new HttpClient()) 
    { 
     apiServer.BaseAddress = new Uri("http://localhost:52126"); 
     apiServer.DefaultRequestHeaders.Accept.Clear(); 
     apiServer.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
     ShipmentGetCarrierModel model = new ShipmentGetCarrierModel(); 
     model.nOrderNumber = 100; 

     // New code: 
     HttpResponseMessage response = 
      apiServer.PostAsync("api/CPC/Get", 
      model, 
     new JsonMediaTypeFormatter()).Result; 

     // I was missing this !!! 
     string returnResult = response.Content.ReadAsStringAsync().Result; 
     JavaScriptSerializer JSserializer = new JavaScriptSerializer(); 
     ShipmentGetCarrierModel returnModel = JSserializer.Deserialize<ShipmentGetCarrierModel>(returnResult); 


     if (response.IsSuccessStatusCode) 
     { 
      return Content(returnModel.ToString()); 
     } 
    } 

    return Content("NOT " + orderNumber.ToString()); 
}