简体   繁体   English

JSON 返回的是 ASP.NET 中的空 CORE WEB API

[英]JSON returned is empty in ASP.NET CORE WEB API

I am trying to learn ASP.NET Web API. I have designed the code something like, when a request(GET) comes to the controller, SP is triggered and the data is converted from datasets to data.table and returned in JSON format.我正在尝试学习 ASP.NET Web API。我设计的代码类似于,当请求(GET)到达 controller 时,触发 SP,数据从数据集转换为 data.table 并以 881569900.78588 格式返回The problem is when I try to return the JSON the Response becomes empty.问题是当我尝试返回 JSON 时,响应变为空。

C# Code: C# 代码:

    [HttpGet]
    public IActionResult GetTodoItem()
    {
        try
        {
            DbConnect dbConnect = new DbConnect();
            DataSet ds = dbConnect.ExecuteSP2("View_Contacts");
            string results = dbConnect.DataTableToJSONWithJSONNet(ds.Tables[0]);
            dynamic json1 = JsonConvert.DeserializeObject(results);
            return this.Ok(json1);
        }
        catch (Exception ex)
        {
        return StatusCode(500,ex.Message);
        }

    }

The Response:响应:

Sample JSON Required:样品 JSON 要求:

[
  {
    "Contact": 6860,
    "Title_Movie": "Ayan",
    "Email_": "",
    "PhoneNumber": null,
    "Job": null,
    "Fax": null,
    "Picture_maxi": "",
    "Type": 2,
    "Company_Id": null,
    "School_Id": null,
    "Theatre_Id": null,
    "FirstName": "Stuart",
    "LastName": "",
    "PreferredName": null,
    "Status": 9,
    "Meter_Id": null,
    "Car_Id": null,
    "Business_Id": null
  }]

How can I return this sample json. Thanks我怎样才能退回这个样品 json。谢谢

  1. Data Tables have an extension method to return them as IEnumerable.数据表有一个扩展方法可以将它们作为 IEnumerable 返回。
  2. If you give the Ok Action an IEnumerable it will return a JSON Object.如果给 Ok Action 一个 IEnumerable,它将返回 JSON Object。

Following this, you can change your method to be like this in order to receive the required output:在此之后,您可以将方法更改为这样以接收所需的 output:

[HttpGet]
    public IActionResult GetTodoItem()
    {
        try
        {
            DbConnect dbConnect = new DbConnect();
            DataSet ds = dbConnect.ExecuteSP2("View_Contacts");
            IEnumerable results = ds.Tables[0].AsEnumerable();
            return this.Ok(results);
        }
        catch (Exception ex)
        {
        return StatusCode(500,ex.Message);
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM