第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 .net core MVC 中創(chuàng)建 JSON

在 .net core MVC 中創(chuàng)建 JSON

C#
慕田峪7331174 2022-01-15 16:42:35
我是 .net 核心編程的新手。我在如何格式化我的 Json 對象時遇到問題。我想創(chuàng)建一個 Json 對象,并且該 JSON 的字段之一將處理一個 Json 對象。我正在使用 MVC 架構(gòu)。謝謝這是我的代碼。    public async Task<List<Schedule>> getAllScheds(){        dynamic response = new JObject();        try {            var data = await _context.MlfbSchedules                        .Include(h => h.Team1)                        .Include(a => a.Team2)                        .Include(s => s.StadiumID)                        .ToListAsync();            return data;        } catch (Exception e) {            response.Error = e.ToString();            return response;        }    }此函數(shù)正在返回此數(shù)據(jù)。 [     {         "scheduleId": 43,         "team1": {             "teamId": 1,             "city": "Virginia",             "teamName": "Armada",             "sImage": "/images/teams-logo-small/virginia.png",             "image": "/images/teams-logo/virginia.png",             "helmetImage": "/images/teams-helmet/virginia.png",             "createdBy": null,             "createdDate": "2016-06-22T10:03:35.58",             "modifiedBy": null,             "modifiedDate": null,             "isDeleted": null         },         "team2": {             "teamId": 3,             "city": "Florida",             "teamName": "Fusion",             "sImage": "/images/teams-logo-small/florida.png",             "image": "/images/teams-logo/florida.png",             "helmetImage": "/images/teams-helmet/florida.png",             "createdBy": null,             "createdDate": "2016-06-22T10:03:35.58",             "modifiedBy": null,             "modifiedDate": null,             "isDeleted": null         },
查看完整描述

1 回答

?
慕絲7291255

TA貢獻(xiàn)1859條經(jīng)驗 獲得超6個贊

為了格式化響應(yīng),您可以實現(xiàn)自己的 Dto,如下所示:


    public class ResultDto<T> where T : class

{

    public ResultDto(string status, IList<T> data)

    {

        Status = status;

        Data = data;

    }

    public string Status { get; set; }

    public IList<T> Data { get; set; }

}

然后改變你的方法


        public async Task<ResultDto<Product>> getAllScheds()

    {

        dynamic response = new JObject();

        try

        {


            var data = new List<Product>

                     {

                         new Product{ProductId=Guid.NewGuid().ToString(),Name="142525"},

                         new Product{ProductId=Guid.NewGuid().ToString(),Name="122555"},

                         new Product{ProductId=Guid.NewGuid().ToString(),Name="125255"}

                     };

            return new ResultDto<Product>("success", data);

        }

        catch (Exception e)

        {

            response.Error = e.ToString();

            return response;

        }


    }


查看完整回答
反對 回復(fù) 2022-01-15
  • 1 回答
  • 0 關(guān)注
  • 325 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號