我们可以通过使用依赖注入来实现这个目标。在我们的目标控制器中注入(delegate)需要调用的控制器,并在需要时调用该控制器的方法。以下是一个示例:
public class MyController : Controller
{
private readonly MyOtherController _otherController;
public MyController(MyOtherController other)
{
_otherController = other;
}
public IActionResult MyAction()
{
// 调用 MyOtherController 的方法
var result = _otherController.OtherAction();
return result;
}
}
public class MyOtherController : Controller
{
public IActionResult OtherAction()
{
return View();
}
}
在上面的代码中,我们首先通过依赖注入将我们需要调用的 MyOtherController
注入到 MyController
中。然后,在需要调用 MyOtherController
中的方法时,我们只需调用 _otherController.OtherAction()
即可。