1
私は、asp.net webapi restアプリケーションをセットアップし、消費し、別のプロジェクトからそれを消費しようとしています。webapi repsonseをmvc4 viewmodelにデシリアライズする
私は私がサービスから返されたデータは以下のように見ている
private IEnumerable<CustomerModel> CustomerDetails()
{
var json = ApiRestHelper.GetApiResponse("Customer/Get");
var data = JsonConvert.DeserializeObject<CustomerViewModel>(json, new JsonSerializerSettings
{
});
のように別のプロジェクトで私のコントローラからそれを消費しています
public static string GetApiResponse(string apiMethod,Dictionary<string,string>queryString=null)
{
using (var client = new WebClient())
{
client.Headers.Add("ApiKey", ConfigurationManager.AppSettings["ApiKey"]);
//add any query string values into the client
if (queryString != null)
{
foreach (var query in queryString)
{
client.QueryString.Add(query.Key, query.Value);
}
}
try
{
string url = string.Format("{0}{1}", ConfigurationManager.AppSettings["ApiBaseUrl"],apiMethod);
return(client.DownloadString(url));
}
catch (Exception ex)
{
return ex.Message;
}
}
}
のようなサービスを呼び出すための簡単なヘルパーを作りました
[{"CustomerId":"24a62bf8-7a4e-4837-859d-1f04dc983011","FirstName":"Joe","LastName":"Bloggs","StoreCustomerId":null}]
私のCustomerViewModelは
ですpublic class CustomerViewModel
{
public IEnumerable<CustomerModel> Customers { get; set; }
}
返されるデータが配列で、リストに変換しようとしています。エラーが発生する
JSON配列([1,2,3])をタイプ「WebApplication.Models.ViewModels.CustomerViewModel」にデシリアライズできません。
逆シリアル化された型は、配列であるか、IEnumerable、ICollection、IListなどのコレクションインターフェイスを実装する必要があります。
ビューモデルへの直列化を解除するには何を変更する必要がありますか?