[英]How can I return a JSON object and status code (IHttpActionResult) from an ExceptionHandler in Web API?
[英]How to convert an IHttpActionResult (with JSON inside) to an object I can process with LINQ
这可能是一个菜鸟问题或架构上的误解,但无论如何我都会问它,因为我没有想法和搜索词:目标是实现一个控制器 CountryController CountriesController()
,它应该连接两个端点的(JSONish)结果。
假设我有两个端点api/allowedCountriesToSell
和api/allowedCountriesToBuy
,它们分别实现为CountriesSellController()
和CountriesBuyController()
。 它们都以 JSON 形式返回数据,我想将其合并并作为新端点提供。 我知道这种架构并不理想,但不允许我在架构上有所不同。 此外,我实际上必须将两个不同的文件发布到这些端点 - 两个现有的控制器都包含类似
[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase file, string selectBox)
{ // ...
我的新端点编译了所有这两个必需的参数,我们称它们为myFileX
和mySelectBox
。 这是我到目前为止所拥有的:
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);
问题是list1
和list2
都是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.