nte*_*dka 6 client json getjson asp.net-mvc-4 asp.net-web-api
我有一个使用MVC4的项目,我想问一下如何从webapi获取数据并返回视图.
模型
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; }
}
Run Code Online (Sandbox Code Playgroud)
我可以Index.cshtml使用此代码列出所有内容
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);
}
Run Code Online (Sandbox Code Playgroud)
我可以回复我的看法.现在我有另一个名为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);
}
Run Code Online (Sandbox Code Playgroud)
对于这个细节,我的Json看起来像这样:
application/json, text/json
{
"NameId": 1,
"FirstName": "This is First Name",
"LastName": "This is Last Name",
"CreatedBy": "This is Created By"
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我收到此错误:
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.
Run Code Online (Sandbox Code Playgroud)
我如何解决这个问题,我是webapi的新手.我想知道为什么如果我列出所有(索引,我使用api/get)它的工作原理,但当我想详细显示它,它不起作用.
谢天谢地
问候
编辑
当我调试
model.Names = response.Content.ReadAsAsync<IList<Name>>().Result.ToList();
Run Code Online (Sandbox Code Playgroud)
它说Null,当我试图得到回应时有什么不对吗?
问题在于:
{
"NameId": 1,
"FirstName": "This is First Name",
"LastName": "This is Last Name",
"CreatedBy": "This is Created By"
}
Run Code Online (Sandbox Code Playgroud)
无法反序列化为 IList。上面的 JSON 只是一个名称,而不是名称的集合。所以Json.NET反序列化会失败。
确保您的 Web API 控制器返回 IList,或更改您的 MVC 代码以将内容读取为单个名称。JSON 中的名称集合将如下所示:
[{
"NameId": 1,
"FirstName": "This is First Name",
"LastName": "This is Last Name",
"CreatedBy": "This is Created By"
},
{
"NameId": 2,
"FirstName": "This is First Name",
"LastName": "This is Last Name",
"CreatedBy": "This is Created By"
}]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4382 次 |
| 最近记录: |