Вернуть результат из другого apicontroller

1

Привет, я пытаюсь вызвать метод Get в моем CategoryController из моего WareController. Как я это сделаю.

Я пробовал это в своем 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;
    }
Теги:
asp.net-mvc

1 ответ

1
Лучший ответ

API-контроллер - это просто классы, поэтому вы легко можете это сделать:

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

Но могут возникнуть проблемы, если вы хотите иметь дело с контекстом.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню