我有一個項目使用MVC4,我想問如何從webapi獲取數據並返回視圖。如何在控制器中調用web api並返回視圖MVC4
型號
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; }
}
我可以使用此代碼
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);
}
我能回到我的觀點列出所有在我Index.cshtml
。現在我有一個名爲Details.cshtml另一種觀點與此代碼:
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);
}
對於這個細節,我的Json如下:
application/json, text/json
{
"NameId": 1,
"FirstName": "This is First Name",
"LastName": "This is Last Name",
"CreatedBy": "This is Created By"
}
當我運行它,我得到這個錯誤:
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.
我該如何解決這個問題,即時通訊新的webapi。我想知道爲什麼如果我列出所有(對於索引,我使用api/get)它的工作原理,但是當我想詳細顯示它時,它不起作用。
感謝幫助
問候
編輯
,當我在
model.Names = response.Content.ReadAsAsync<IList<Name>>().Result.ToList();
其說空調試,有什麼不對,當我嘗試得到的迴應?
我認爲這是我的問題。但我不做webapi,我只是在我的mvc中使用它。如何列出該json?感謝bro – 2013-02-09 04:37:06
,然後讀取內容作爲名稱,而不是IList如果您不控制Web服務... –
2013-02-09 04:46:52
我使用 var NamesId = response.Content.ReadAsAsync().Result; 當我調試NamesId,我可以得到結果。但我怎麼能返回到Details.cshtml視圖? –
2013-02-09 04:48:06