asp.net-mvc 我们可以在asp.net MVC 中从另一个控制器调用控制器的方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2910948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:20:24  来源:igfitidea点击:

Can we call the Method of a controller from another controller in asp.net MVC?

asp.net-mvc

提问by SAHIL SINGLA

Can we call the Method of a controller from another controller in asp.net MVC?

我们可以在asp.net MVC 中从另一个控制器调用控制器的方法吗?

回答by Nick Masao

You could also simply redirect straight to the method like so:

您也可以简单地直接重定向到该方法,如下所示:

public class ThisController 
{
    public ActionResult Index() 
    {
       return RedirectToAction("OtherMethod", "OtherController");
    }
}

回答by SAHIL SINGLA

Technically, yes. You can call a static method of a controller or initialize an instance of a controller to call its instance methods.

从技术上讲,是的。您可以调用控制器的静态方法或初始化控制器的实例以调用其实例方法。

This, however, makes little sense. The methods of a controller are meant to be invoked by routing engine indirectly. If you feel the need to directly call an action method of another controller, it is a sign you need some redesign to do.

然而,这没有什么意义。控制器的方法旨在由路由引擎间接调用。如果您觉得需要直接调用另一个控制器的操作方法,则表明您需要进行一些重新设计。

回答by Igor Zevaka

Well, there are number of ways to actually call an instance method on another controller or call a static method off that controller type:

好吧,有多种方法可以实际调用另一个控制器上的实例方法或调用该控制器类型的静态方法:

public class ThisController {
  public ActionResult Index() {
    var other = new OtherController();
    other.OtherMethod();
    //OR
    OtherController.OtherStaticMethod();
  }
}

You could also redirect to to another controller, which makes more sense.

您还可以重定向到另一个控制器,这更有意义。

public class ThisController {
  public ActionResult Index() {
    return RedirectToRoute(new {controller = "Other", action = "OtherMethod"});
  }
}

Or you could just refactor the common code into its own class, which makes even more sense.

或者您可以将公共代码重构为它自己的类,这更有意义。

public class OtherClass {
  public void OtherMethod() {
    //functionality
  }
}

public class ThisController {
  public ActionResult Index() {
    var other = new OtherClass();
    other.OtherMethod();
  }
}

回答by ShaileshDev

As controllers are just classes: Yes, we can do it. We can do it by some of the following ways:

由于控制器只是类:是的,我们可以做到。我们可以通过以下一些方式来做到这一点:

  1. By directly redirecting- return RedirectToAction("MethodName", "ControllerName");

  2. By creating object - ControllerName objController=new ControllerName();objController.methodName(parameters)

  1. 通过直接重定向 - return RedirectToAction("MethodName", "ControllerName");

  2. 通过创建对象 - ControllerName objController=new ControllerName();objController.methodName(parameters)

回答by Abdullah Yousuf

Try This.

尝试这个。

var ctrl= new MyController();
ctrl.ControllerContext = ControllerContext;
//call action
return ctrl.Action();

回答by Shiv Ratan Kumar

Yes, you can call a method of another controller.

是的,您可以调用另一个控制器的方法。

public ActionResult Index()
        {
            AccountController accountController = new AccountController {ControllerContext = ControllerContext};
            return accountController.Index();
        }

The controller is also a simple class. Only things are that its inheriting Controller Class. You can create an object of the controller, but it will not work for Routing if you want to redirect to another page.

控制器也是一个简单的类。唯一的事情是它继承了控制器类。您可以创建控制器的对象,但如果您想重定向到另一个页面,它将不适用于路由。