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

后端 未结 6 1060
攒了一身酷
攒了一身酷 2021-02-07 10:45

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

6条回答
  •  借酒劲吻你
    2021-02-07 11:08

    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.

提交回复
热议问题