MVC ControllerActionInvoker 和调用操作
是否可以使用自定义操作调用程序而无需在控制器处理程序工厂中实例化它?例如在自定义控制器工厂中:
IController IControllerFactory.CreateController(RequestContext reqContext, string controllerName)
{
var controller = base.CreateCOntroller(reqContext,controllerName ) as Controller;
controller.ActionInvoker = new CustomActionInvoker();
}
或者是否有另一种方法可以执行 MVC 操作而无需使用自定义操作调用程序?
更新问题
我有一个控制器,例如 HomeController
和 Index
操作。 Index
是控制器中的主要操作。一旦执行了 Index 操作,MVC 视图将使用 Ajax - GET 请求(我们使用 jTemplates)触发多个操作。
示例
// Controller actions
// main action and View
public ActionResult Index() { ... }
public ActionResult AjaxAction1(string id) { ... }
public ActionResult AjaxAction2() { ... }
public ActionResult AjaxAction3() { ... }
现在我想过滤其中一些操作,以根据某些情况不执行。例如,当id
等于2时,我想停止执行AjaxAction1
。
回到我原来的问题。有没有一种方法可以在不使用操作调用程序的情况下实现这一目标。我不想使用操作调用程序的原因是我的项目的结构方式最终导致了循环引用。
任何想法都非常感激。
Is it possible to use a custom action invoker without having to instantiate it in the Controller handler factory? For example in custom controller factory:
IController IControllerFactory.CreateController(RequestContext reqContext, string controllerName)
{
var controller = base.CreateCOntroller(reqContext,controllerName ) as Controller;
controller.ActionInvoker = new CustomActionInvoker();
}
Or is there another way I can execute an MVC action without having to use a custom action invoker?
Updating the question
I have a controller, say HomeController
and Index
action. Index
is the main action in the controller. Once the Index
action gets executed, the MVC view will fire multiple actions using Ajax - GET requests (we using jTemplates).
Example
// Controller actions
// main action and View
public ActionResult Index() { ... }
public ActionResult AjaxAction1(string id) { ... }
public ActionResult AjaxAction2() { ... }
public ActionResult AjaxAction3() { ... }
Now I want to filter some of these actions not to execute depending on certain scenarios. For example I want to stop executing AjaxAction1
when the id
is equal to 2.
Back to my original question. Is there a way to achieve this without using the action invoker. The reason that i don't want to use the action invoker is the way my project being structured ended up with circular references.
Any ideas greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
找到答案,您可以子类化 Controller 并在那里创建 ControllerActionInvoker。
Found the answer you can subclass the Controller and create the ControllerActionInvoker there.
使用操作方法选择器
取决于
id
等于 2 时发生的情况,但这可以通过编写自定义操作方法选择器轻松完成。使用操作方法选择器,您可以提供根据参数值执行的操作:
正如您从本示例中看到的,自定义操作方法选择器采用两个参数:
id
)操作方法选择器将来自客户端的所有路由值视为字符串,因此您实际上可以使用正则表达式来实现此目的。这也使得它非常非常灵活。
第一个操作方法将在
id == "2"
时执行,第二个操作方法在id != "2"
时执行。Using action method selector
Depending on what happens when
id
equals 2, but this could quite easily be done by writing a custom action method selector.Using action method selector you could provide your actions that would execute depending on your parameter values:
As you can see from this example the custom action method selector takes two parameters:
id
in example)Action method selector sees all route values as strings when they're coming from the client so you can actually pull this off by using regular expressions. And it makes it also very very flexible.
The first action method will get executed when
id == "2"
and the second one whenid != "2"
.