2015-12-04 99 views
1

我在某些WebAPI上使用HttpClient。HTTP通過PostAsJsonAsync發佈多個對象

我需要多個對象發送到POST,這裏的如何我的帖子方法聲明:

public string Post(Models.Client value, App.ControlCenter.Models.Company c) 
{ 
    ... 
} 

這裏就是我如何打電話到的WebAPI:

using (var client = new HttpClient()) 
{ 
    client.BaseAddress = new Uri("http://localhost/"); 
    client.DefaultRequestHeaders.Accept.Clear(); 
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

    var s = client 
     .PostAsJsonAsync("api/Client/", c) 
     .Result 
     .Content.ReadAsAsync<App.ControlCenter.Models.RolDTO>().Result; 
    return View(); 
} 

我需要什麼要做的是發送客戶端對象和公司的Post方法工作。

回答

1

客戶端

你需要創建一個DTO類的類型Models.Client兩個屬性App.ControlCenter.Models.Company

public class DTO.ComplexObject 
{  
    public Models.Client tClientModel { get; set; } 

    public App.ControlCenter.Models.Company tCompany{ get; set; } 
} 

然後填寫ComplexObject對象(即TComplexObject)並使用

HttpResponseMessage tResponse = tHttpClient.PostAsJsonAsync(url,TComplexObject).Result; 

API

[HttpPost] 
public HttpResponseMessage AddData(DTO.ComplexObject tComplexObject) 
{