Make names of named tuples appear in serialized JSON responses

后端 未结 4 1639
借酒劲吻你
借酒劲吻你 2021-02-05 05:31

Situation: I have multiple Web service API calls that deliver object structures. Currently, I declare explicit types to bind those object structures together. F

4条回答
  •  一向
    一向 (楼主)
    2021-02-05 05:41

    The simplest solution is using dynamic code, i.e. C#'s ExpandoObject to wrap your response in the format you expect the API to have

        public JsonResult GetSomething(int param)
        {
            var (speed, distance) = DataLayer.GetData(param);
            dynamic resultVM = new ExpandoObject();
            resultVM.speed= speed;
            resultVM.distance= distance;
            return Json(resultVM);
        }
    

    The return type of "GetData" is

    (decimal speed, int distance)
    

    This gives a Json response in the way you expect it to

提交回复
热议问题