How to convert an IHttpActionResult (with JSON inside) to an object I can process with LINQ

喜欢而已 提交于 2020-01-14 02:07:26

问题


It might be a noob question or an architectural misunderstanding, but I ask it anyhow because I am out of ideas and search terms: The goal is to implement a controller CountriesController() which is supposed to concatenate the (JSONish) results of two endpoints.

Assume I have two endpoints api/allowedCountriesToSell and api/allowedCountriesToBuy which are implemented as CountriesSellController() and CountriesBuyController() respectively. Both of them give back data as JSON which I want to merge and offer as a new endpoint. I am aware that this architecture is not ideal, but I am not allowed to do it architecturally different. Furthermore, I actually have to POST two different files to those endpoints - both existing controllers contain something like

[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase file, string selectBox)
{ // ...

My new endpoint compiles all these two required parameters, let's call them myFileX, and mySelectBox. Here what I have have so far:

var myOtherContoller1 = new CountriesSellController();
var list1 = myOtherContoller1.FileUpload(myFile1,mySelectBox);

var myOtherContoller2 = new CountriesSellController();
var list2 = myOtherContoller1.FileUpload(myFile2,mySelectBox);

my result = list1.asEnumerable().Concat(list2.asEnumerable()); // Pseudocode. Here I am lost.

return Ok(result);

The problem is that both list1 and list2 are of type IHttpActionResult and I am not sure how to extract the data inside that. Ideally, result would be of type IEnumerable<UploadStatusDto> where I define the respective data transfer object as

namespace API.Models
{
    public class UploadStatusDto
    {
        public int UploadId { get; set; } // contained in the response of both controllers 
        public string FileName { get; set; } // myFileX - parameter for calling the 2 existing controllers
        public int UploadStatus { get; set; } // coming back within listX
        public int Type { get; set; } // whether it is a buy or a sell, i.e. which controller I called
    }

Any guidance is appreciated.


回答1:


You need to do something line this.

var response = await myOtherContoller1.FileUpload(myFile2,mySelectBox).ExecuteAsync();

This will return HttpResponseMessage and you can get the content from it

You can get your content like this: Getting content/message from HttpResponseMessage.

My suggestion though, would be to extract the logic of your other controllers to a service class, and call both in this and the other two, the logic that is now in the original controllers.



来源:https://stackoverflow.com/questions/59198088/how-to-convert-an-ihttpactionresult-with-json-inside-to-an-object-i-can-proces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!