簡體   English   中英

在 MVC .Net 中使用外部 Web API

[英]Consume external Web API in MVC .Net

我需要使用外部 Web API,以便以 GRID 格式搜索和查看數據。

到目前為止,這是我的代碼:

***Model***

public class AlertMessage
{
    public string Title { get; set; }
    public string Message { get; set; }
    public string Severity { get; set; }
}

public class ItemLine
{
    public string Description { get; set; }
    public int BilledAmount { get; set; }
    public int PaidAmount { get; set; }
}

public class CitationDetail
{
    public string CitationNumber { get; set; }
    public string DefendantName { get; set; }
    public string DateofBirth { get; set; }
    public string VehicleTagNumber { get; set; }
    public string CaseType { get; set; }
    public string CaseStatus { get; set; }
    public string AppearanceDate { get; set; }
    public bool IsPayable { get; set; }
    public int FineSuspended { get; set; }
    public int FineServed { get; set; }
    public List<AlertMessage> AlertMessages { get; set; }
    public List<ItemLine> ItemLines { get; set; }
}

public class Root
{
    public List<CitationDetail> CitationDetails { get; set; }
    public int CitationCount { get; set; }
    public bool SuccessfulSearch { get; set; }
    public string ErrorMessage { get; set; }
}

**Controller**

    string Baseurl = "http://10.241.2.68:8109/";
    public async Task<ActionResult> Index()
    {
        List<CitationDetail> CitInfo = new List<CitationDetail>();
        using (var client = new HttpClient())
        {
            //Passing service base url
            client.BaseAddress = new Uri(Baseurl);
            client.DefaultRequestHeaders.Clear();
            //Define request data format
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
           
            HttpResponseMessage Res = await client.GetAsync("api/Juris/Citation/7082^");
            //Checking the response is successful or not which is sent using HttpClient
            if (Res.IsSuccessStatusCode)
            {
                //Storing the response details recieved from web api
                var EmpResponse = Res.Content.ReadAsStringAsync().Result;
      //Deserializing the response recieved from web api and storing into the citation list
                CitInfo = JsonConvert.DeserializeObject<List<CitationDetail>>(EmpResponse);
            }
            //returning the citation list to view
            return View(CitInfo);
        }
    }

我目前收到此錯誤:


無法將當前 JSON 對象(例如 {"name":"value"})反序列化為類型“System.Collections.Generic.List`1[CPA.Models.CitationDetail]”,因為該類型需要一個 JSON 數組(例如 [1, 2,3]) 以正確反序列化。 要修復此錯誤,請將 JSON 更改為 JSON 數組(例如 [1,2,3])或更改反序列化類型,使其成為正常的 .NET 類型(例如,不是像整數這樣的原始類型,而不是像這樣的集合類型)可以從 JSON 對象反序列化的數組或列表)。 JsonObjectAttribute 也可以添加到類型中以強制它從 JSON 對象反序列化。 路徑“CitationDetails”,第 1 行,位置 19。

 ***JSON*****
{
"CitationDetails": [
    {
        "CitationNumber": "00000708244",
        "DefendantName": ",  ",
        "DateofBirth": "",
        "VehicleTagNumber": "",
        "CaseType": "CITATION",
        "CaseStatus": "",
        "AppearanceDate": "",
        "IsPayable": true,
        "FineSuspended": 0,
        "FineServed": 0,
        "AlertMessages": [],
        "ItemLines": [
            {
                "Description": "Fine Amount",
                "BilledAmount": 0,
                "PaidAmount": 0
            }
        ]
    }
],
"CitationCount": 1,
"SuccessfulSearch": true,
"ErrorMessage": ""

}

在此處輸入圖片說明

我看不到你的輸出 json 文件,所以我最好的猜測是替換

   var EmpResponse = Res.Content.ReadAsStringAsync().Result;
 CitInfo = JsonConvert.DeserializeObject<List<CitationDetail>>(EmpResponse);

經過

 var empResponse = await Res.Content.ReadAsStringAsync();
var empResponseObj = JsonConvert.DeserializeObject<Root>(empResponse);
 .......

 return View(empResponseObj.CitationDetails);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM