从 ActionB 调用 ActionA,然后继续执行 ActionB
我想我对 MVC 有一些误解。我正在尝试执行以下操作:
public class ControllerA : Controller
{
public ActionResult Index()
{
// do code
// perform action on ControllerB - something like:
// RedirectToAction("Action", "ControllerB");
// CARRY ON with more code
}
}
public class ControllerB : Controller
{
public void Action()
{
// do code
}
}
显然 RedirectToAction("Action", "ControllerB");
不起作用。那么我该怎么做呢?我想我可以让所有需要使用 Action() 的控制器从 ControllerB 继承,但这感觉是一种非常糟糕的方法。请帮忙!
I think I misunderstand something about MVC. I'm trying to do the following:
public class ControllerA : Controller
{
public ActionResult Index()
{
// do code
// perform action on ControllerB - something like:
// RedirectToAction("Action", "ControllerB");
// CARRY ON with more code
}
}
public class ControllerB : Controller
{
public void Action()
{
// do code
}
}
Obviously RedirectToAction("Action", "ControllerB");
isn't working. So how do I do it? I guess I could have all controllers that need to use Action() inherit from ControllerB but that feels a really bad way to do it. Please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从 RedirectToAction() 返回 ActionResult
如果您希望 RedirectToAction 实际上重定向到某个操作,则必须 。在您澄清“不起作用”对您意味着什么之后,我认为您应该让所有控制器都从基础继承。这是一个非常标准的方法。
You have to return the ActionResult from RedirectToAction()
is what you need to do if you want RedirectToAction to actually redirect to an action. After you clarified what "isn't working" means to you I think you should just have all controllers inherit from a base. That is a pretty standard approach.
我相信您当前正在执行的控制器是该类的一个实例,因此您需要创建一个控制器 B 的实例才能在其上执行任何操作,因此如果没有黑客。
不过,我认为有两种方法可以更好地获得我认为您想要的结果:
1)创建一个“ControllerBase”类,并让所有控制器继承它而不是“Controller”,然后您可以添加到基类中的任何共享代码(也许作为静态方法)然后所有控制器都可以作为成员轻松轻松地访问它。
2)由于MVC将创建一个直接的DLL,您可以根据需要添加新的类,例如添加一个新的项目文件夹,如“Globals”,添加一个名为“Funcs”的新类文件,然后您就有一个可以访问的静态库从任何地方,Funcs.SendEmail();等等
如果我偏离了目标,好吧!无论如何,编码愉快,呵呵
I believe the controller you are currently executing is an instance of the class so you would need to make an instance of controller B to be able to execute anything on it, so what you are trying to do there just won't really work without a hack.
I think however there is 2 methods to better get the results i think you are after:
1) Make a 'ControllerBase' class and have all controllers inherit from it instead of from 'Controller' then any shared code you can add into the base class (as a static method perhaps) and then all controllers can access it as a member nice and easy.
2) As MVC will make a straight up DLL you can add in new classes as you need, eg add a new project folder like 'Globals' add a new class file called 'Funcs' and there you have a static lib that you can access from anywhere, Funcs.SendEmail(); etc
If i'm off the mark ok! happy coding anyway heh
我已将控制器工厂中的工厂方法作为委托(Func CreateController)注入控制器,并使用它来创建子控制器,例如在这种情况下。有很多方法可以实现您的目标,但我认为这可能是实现您想要的目标的快速方法。
I have injected controllers with a factory method from the controller factory as a delegate (Func CreateController), and used that to create sub-controllers, such as in this circumstance. There's plenty of ways to accomplish your goal, but I think that might be quick way to get what you want working.