2013-02-09 33 views
6

MVC4 kullanarak bir projem var, webapi'den nasıl veri alacağınızı ve görüntüye nasıl dönüleceğini sormak istiyorum.denetleyicideki web api'yi nasıl çağırır ve görünüme geri döner MVC4

Modeli

public class Name 
{ 
    public Int32 NameId { get; set; } 
    public String FirstName{ get; set; } 
    public String LastName{ get; set; } 
    public String CreatedBy { get; set; } 
} 

public class IListMyProject 
{ 
    public List<Name> Names { get; set; } 
} 

Ben görüşümü dönebilirsiniz Bu kodu

public ActionResult Index() 
    { 
     string securityToken = repo.GetTokens(); 
     if (securityToken != null) 
     { 
      HttpClient client = new HttpClient(); 
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

      HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "webapiurl/api/Name/Get?$orderby=LastName&$top=10"); 
      string authHeader = System.Net.HttpRequestHeader.Authorization.ToString(); 
      httpRequestMessage.Headers.Add(authHeader, string.Format("JWT {0}", securityToken)); 
      var response = client.SendAsync(httpRequestMessage) 
       .ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode()) 
       .Result; 
      if (response.IsSuccessStatusCode) 
      { 
       model.Names = response.Content.ReadAsAsync<IList<Name>>().Result.ToList(); 

      } 
     } 
     return View("Index", model); 
    } 

kullanarak benim Index.cshtml tüm listeleyebilirsiniz. ve şimdi ben başka bu kodla Details.cshtml denilen görünümü vardır: i çalıştırdığınızda

application/json, text/json 
{ 
    "NameId": 1, 
    "FirstName": "This is First Name", 
    "LastName": "This is Last Name", 
    "CreatedBy": "This is Created By" 
} 

i olsun bu hata:

bu Detay için

public ActionResult Details(string id) 
    { 
     string securityToken = repo.GetTokens(); 
     if (securityToken != null) 
     { 
      HttpClient client = new HttpClient(); 
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

      HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, "webapiurl/api/Name/GetById/"+id+""); 
      string authHeader = System.Net.HttpRequestHeader.Authorization.ToString(); 
      httpRequestMessage.Headers.Add(authHeader, string.Format("JWT {0}", securityToken)); 

      var response = client.SendAsync(httpRequestMessage) 
       .ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode()) 
       .Result; 
      if (response.IsSuccessStatusCode) 
      { 

       model.Names = response.Content.ReadAsAsync<IList<Name>>().Result.ToList(); 

      } 
     } 
     return View(model); 
    } 

, benim Json şöyle görünür

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IList`1[Models.Name]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. 

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. 

Path 'NameId', line 1, position 10. 

Bunu nasıl düzeltirim, webapi için yeni. Merak ediyorum (indeks, api/get kullanıyorum) tümünü listeleyip niçin ayrıntılı olarak göstermek istediğimde işe yaramıyor. Yardım için

teşekkür

Selamlar

i

model.Names = response.Content.ReadAsAsync<IList<Name>>().Result.ToList(); 

onun boş diyor debug i yanıt almak çalıştığınızda sorun var DÜZENLEME

?

{ 
    "NameId": 1, 
    "FirstName": "This is First Name", 
    "LastName": "This is Last Name", 
    "CreatedBy": "This is Created By" 
} 

bir IList olarak seriden edilemez:

cevap