![](/img/trans.png)
[英]HttpContent.ReadAsAsync method not returning correct value from a Web API call
[英]HttpContent ReadAsAsync map data coming from api c#
我正在進行服務器到服務器的通信,並且正在從api中獲取數據。 我使用Http Web客戶端來做到這一點。 這是我的代碼
public async Task<List<Report>> GetReports(string tokenType, string token, int page, int count)
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(tokenType, token);
var builder = new UriBuilder(ApiEndPointBase + "api/" + ApiVersion + "/LibraryDocument");
builder.Port = -1;
var query = HttpUtility.ParseQueryString(builder.Query);
query["page"] = page.ToString();
query["count"] = count.ToString();
builder.Query = query.ToString();
string url = builder.ToString();
var result = await client.GetAsync(url);
if (!result.IsSuccessStatusCode)
throw new Exception("");
List<Report> reports = new List<Report>();
await result.Content.ReadAsAsync<List<Report>>().ContinueWith(response =>
{
reports = response.Result;
});
return reports;
}
我遇到的問題是我正在以這種格式從服務器獲取數據
public class Report
{
public int pK_LibraryDocument { get; set; }
public string fileName { get; set; }
public List<string> AvailableForModules { get; set; }
}
但是我想要這樣的數據
public class Report
{
public int id{ get; set; }
public string fileName { get; set; }
public List<string> AvailableForModules { get; set; }
}
我可能還會更改其他變量名。 這樣做的原因是,我將有多個具有相同數據的數據源,但是格式或名稱將不同。 因此,我想對自己進行集中命名。
是否可能以不昂貴(耗時)的方式。
默認情況下用於反序列化的JsonProperty
支持JsonProperty
屬性,用於調整JSON字段名稱:
public class Report
{
[JsonProperty("pK_LibraryDocument")]
public int id { get; set; }
public string fileName { get; set; }
public List<string> AvailableForModules { get; set; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.