从另一个apicontroller返回结果

时间:2014-12-12 10:01:34

标签: c# asp.net-mvc

您好我正在尝试从我的 WareController 中调用 CategoryController 中的获取方法。我该怎么做。

我在 WareController

中尝试了这个
// 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);


我的 CategoryController 看起来像这样

// 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;
    }

1 个答案:

答案 0 :(得分:1)

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

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

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