从另一个 API 控制器返回结果

本文关键字:返回 结果 控制器 API 另一个 | 更新日期: 2023-09-27 18:34:10

嗨,我正在尝试从我的 WareController 调用我的 CategoryController 中的 Get 方法。我该怎么做。

我已经在我的软件控制器中尝试过这个

// GET: api/Ware?category=5
    public List<WareDTO> GetByCategroy(int id)
    {
        BLLServiceGateway<List<WareDTO>> gate = new BLLServiceGateway<List<WareDTO>>();
        var item = gate.Get(path + "?category=" + id);
        if (item == null)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
        return item;
    }
public IHttpActionResult ViewItems(int id)
    {
        var model = new WareModel()
        {
            wares = GetByCategroy(id),
            currentCategory = Redirect("api/category/" + id) /// This is were I want to get the category object
        };
    }
        return Ok(model);


我的类别控制器看起来像这样

// GET: api/Categories/5
    public CategoryDTO Get(int id)
    {
        BLLServiceGateway<CategoryDTO> gate = new BLLServiceGateway<CategoryDTO>();
        var item = gate.Get(path + id);
        if (item == null)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
        return item;
    }

从另一个 API 控制器返回结果

API 控制器只是类,因此您可以轻松地执行此操作:

currentCategory = new CategoryController().Get(id);

但是如果你想处理上下文,可能会有问题。