繁体   English   中英

如何将 IHttpActionResult(包含 JSON)转换为我可以使用 LINQ 处理的对象

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

这可能是一个菜鸟问题或架构上的误解,但无论如何我都会问它,因为我没有想法和搜索词:目标是实现一个控制器 CountryController CountriesController() ,它应该连接两个端点的(JSONish)结果。

假设我有两个端点api/allowedCountriesToSellapi/allowedCountriesToBuy ,它们分别实现为CountriesSellController()CountriesBuyController() 它们都以 JSON 形式返回数据,我想将其合并并作为新端点提供。 我知道这种架构并不理想,但不允许我在架构上有所不同。 此外,我实际上必须将两个不同的文件发布到这些端点 - 两个现有的控制器都包含类似

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

我的新端点编译了所有这两个必需的参数,我们称它们为myFileXmySelectBox 这是我到目前为止所拥有的:

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);

问题是list1list2都是IHttpActionResult类型,我不确定如何提取其中的数据。 理想情况下, result将是IEnumerable<UploadStatusDto>类型,我将相应的数据传输对象定义为

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
    }

任何指导表示赞赏。

你需要做一些事情。

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

这将返回HttpResponseMessage并且您可以从中获取内容

您可以像这样获取内容从 HttpResponseMessage 获取内容/消息

不过,我的建议是将其他控制器的逻辑提取到一个服务类中,并在这个和其他两个控制器中调用现在在原始控制器中的逻辑。

暂无
暂无

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

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