简体   繁体   English

ASP.NET web api 只返回数组中每个对象中的第一个对象

[英]ASP.NET web api returning only first object in each object in a array

startup.cs启动文件

   services.AddMvc().AddJsonOptions(options =>
        {
            options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

below is the sample response json returned by web api application when deployed to production.下面是部署到生产环境时由 web api 应用程序返回的示例响应 json。 In localhost Iam getting data for "lastHourData","lastHourBeforeData" but not in production.在本地主机中,我正在获取“lastHourData”、“lastHourBeforeData”的数据,但不在生产中。

    [{
        "currentData": {
            "id": "213123123esdwq23d",
             "payload": [
                {
                    "key": "sdfsdf",
                    "value": "T"
                }
            ]
        },
        "lastHourData": null,
        "lastHourBeforeData": null
    },
    {
        "currentData": {
            "id": "sdfdsf",
             "payload": [
                {
                    "key": "gf",
                    "value": "T"
                }
            ]
        },
        "lastHourData": null,
        "lastHourBeforeData": null
    }]

below is the method, CurrentData,LastHourData and LastHourBeforeData each has 250 properities.下面是方法,CurrentData、LastHourData 和 LastHourBeforeData 每个都有 250 个属性。

  public async Task<ComepletePanel> GetCompletePanelData(string id)
    {
        var queryRequest = RequstBuilder(id);
        var result = await queryRequest;
        var currentResult = result.Items.Select(Map2).FirstOrDefault();

        var queryRequest2 = getlastHourDataForMasterPanelBuilder(id);
        var result2 = await queryRequest2;
        var currentResult2 = result2.Items.Select(Map2).FirstOrDefault();

        var queryRequest3 = getlastBeforHourDataForMasterPanelBuilder(id);
        var result3 = await queryRequest3;
        var currentResult3 = result3.Items.Select(Map2).FirstOrDefault();

        var completData = new ComepletePanel
        {
            CurrentData = currentResult,
            LastHourData = currentResult2,
            LastHourBeforeData = currentResult3
        };

        return completData;
    }

below is the controller method下面是控制器方法

  public async Task<IEnumerable<ComepletePanel>> GetCustBoilersByCustId2(int id)
    {
        try
        {
            var recordData= await _context.table.ToListAsync();
            List<ComepletePanel> resultData= new List<ComepletePanel>();

            foreach (var b in recordData)
            {
                var CurrentResponse = await _getItem.GetCompletePanelData(b.id);
                resultData.Add(CurrentResponse);
            }

            return resultData;

        }
        catch (Exception ex)
        {
            _logger.LogError($"Failed to get all customers {ex}");
            return null;
        }
    }

You might need to serialize your object.您可能需要序列化您的对象。 Please see this for your reference: https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to请参阅此供您参考: https : //docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to

I think you should refactor your code to look more presentable and appealing to you like this.我认为你应该重构你的代码,让它看起来更像这样,对你更有吸引力。 It might solve your problem.它可能会解决您的问题。

Instead of you writing methods like getLastHourDataForMasterPanelBuilder(string id) and getlastHourDataForMasterPanelBuilder(string id) , first those names look too long and then just summarize your algorithm in one method such that you just pass a time or a logic data or time argument to it then it gets the data based on time you want.不要编写像getLastHourDataForMasterPanelBuilder(string id)getlastHourDataForMasterPanelBuilder(string id) ,首先这些名称看起来太长,然后只用一种方法总结你的算法,这样你就可以向它传递时间或逻辑数据或时间参数它根据您想要的时间获取数据。

public async Task<IEnumerable<CompletePanel>> GetCompleteData(string id params string[] args)
{
    List<QueryRequest> requests = new List<QueryRequest>();

    foreach (string argument in args)
    {
        var queryRequest = RequestBuilder(id);
        var result = await queryRequest;
        var knownResult = result.Items.Select(Map2).FirstOrDefault();
        requests.Add(knownResult);
    }

    return requests;
}

Then in your controller instead of performing a loop you just pass the id and time argument or logical argument as string to the method.然后在您的控制器中而不是执行循环,您只需将 id 和 time 参数或逻辑参数作为字符串传递给方法。

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

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