从另一个控制器调用非操作方法

Calling non-action method from another controller

我是 MVC 的新手,我找不到解决我的问题的方法。我想从另一个控制器调用非操作方法(return 字符串、整数等)。有可能吗?如何正确地做到这一点?我正在使用 ninject,我的控制器如下所示:

public class ShopController : Controller
{
  private IShopRepository repository;

  public ShopController(IShopRepository shopRepository)
  {
    this.repository = shopRepository;
  }

  public int GetShopId(string shopName)
  {
    // how to call this method from another controller?
    // here is linq query which needs shop table repository!
  }
}

抱歉我的英语不好,非常感谢您的回复!:)

我认为最简单的解决方案是从您要使用其方法的控制器继承(前提是该方法不是私有的)。

让控制器调用其他控制器的实例方法并不是最好的设计。我建议您将方法 GetShopId 添加到 IShopRepository 中,以便使用存储库的每个组件都可以访问它(包括您的其他控制器)。 IShopRepository 毕竟是这个方法所属的地方。