Controller间调用 - ASP.NET MVC实现AController调用BController

通过DependencyResolver,实现在AController中调用BController的函数

Controller间调用 - ASP.NET MVC实现AController调用BController

example1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class ChildController
{

public ActionResult Copy(int ID = 0)
{
// ...
// copy a child instance
// ...
}
}

public class ParentController
{

public ActionResult Copy(int ID = 0)
{
// ...

var childController = DependencyResolver.Current.GetService<ChildController>();
var result = childController.Copy(childID);

// ...
}
}

reference

Asp.net mvc 动作方法中 调用 另一个控制器的动作方法 - CSDN

1
2
3
4
5
6
public ActionResult 动作方法()
{
var otherController = DependencyResolver.Current.GetService<另一个控制器的类名>();
var result = otherController.另一个动作方法();
return result;
}