2016-06-24 7 views
1

私のプロジェクトでは、Projectというモデルクラスがあり、Web APIで開発されたRESTサービスにPOSTメソッドがあり、Projectのリストを受け取っていますが、常にこのメソッドをIListパラメータ無効である。リストをパラメータとして渡すために必要なことは何ですか?Web APIのPOSTメソッドのパラメータとしてIList <MyObject>を渡す方法はありますか?

私のコントローラです:

[IdentityBasicAuthentication] 
[Authorize] 
[RoutePrefix("project")] 
public class ProjectController : ApiController 
{ 
    [HttpPost] 
    [Route("listafavoritos")] 
    public IList<PROSPERI_EpmFast.Core.Modelo.POCO.Project> ListaFavoritos([FromBody] List<PROSPERI_EpmFast.Core.Modelo.POCO.Project> projetos) 
    { 
     //execute the actions 
    } 
} 

、それは私のサービスコールです:

public virtual async Task<IList<Project>> ListaProjetosFavoritos(IList<Project> projetosFavoritos) 
    { 


     var url = "http://myserviceurl/project/listafavoritos"; 


     _client.DefaultRequestHeaders.Authorization = authentication; 


     foreach (Project projetoLimpar in projetosFavoritos) 
     { 
      projetoLimpar.Tasks = null; 
      projetoLimpar.Team = null; 
      projetoLimpar.Cost = null; 
      projetoLimpar.Schedule = null; 
     } 

     var uri = new Uri(string.Format(url, string.Empty)); 
     var json = JsonConvert.SerializeObject(projetosFavoritos, this._microsoftDateFormatSettings); 
     var contentEnvio = new StringContent(json, Encoding.UTF8, "application/json"); 

     _client.Timeout = new TimeSpan(0, 5, 0); 
     var response = await _client.PostAsync(uri, contentEnvio); 

     if (response.IsSuccessStatusCode) 
     { 
      var content = await response.Content.ReadAsStringAsync(); 
      return JsonConvert.DeserializeObject<IList<Project>>(content, this._microsoftDateFormatSettings); 
     } 
     return null; 
    } 

答えて

0

が配列

[HttpPost] 
[Route("listafavoritos")] 
public IList<Project> ListaFavoritos([FromBody]Project[] projetos) { ... } 

にパラメータを変換したときも、あなたの配列にデータを変換します送信しています

//.... 

var payload = projetosFavoritos.ToArray(); 
var json = JsonConvert.SerializeObject(payload, this._microsoftDateFormatSettings); 
var contentEnvio = new StringContent(json, Encoding.UTF8, "application/json"); 

_client.Timeout = new TimeSpan(0, 5, 0); 
var response = await _client.PostAsync(uri, contentEnvio); 

//.... 
+0

魅力的な作品です!ありがとうございました。 –

関連する問題