MVC ControllerActionInvoker 和调用操作

发布于 2024-09-13 01:08:08 字数 996 浏览 4 评论 0原文

是否可以使用自定义操作调用程序而无需在控制器处理程序工厂中实例化它?例如在自定义控制器工厂中:

IController IControllerFactory.CreateController(RequestContext reqContext, string controllerName)
{
    var controller = base.CreateCOntroller(reqContext,controllerName ) as Controller;
    controller.ActionInvoker = new CustomActionInvoker();
}

或者是否有另一种方法可以执行 MVC 操作而无需使用自定义操作调用程序?

更新问题

我有一个控制器,例如 HomeControllerIndex 操作。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转身以后 2024-09-20 01:08:08

找到答案,您可以子类化 Controller 并在那里创建 ControllerActionInvoker。

Found the answer you can subclass the Controller and create the ControllerActionInvoker there.

就是爱搞怪 2024-09-20 01:08:08

使用操作方法选择器

取决于 id 等于 2 时发生的情况,但这可以通过编写自定义操作方法选择器轻松完成。

使用操作方法选择器,您可以提供根据参数值执行的操作:

[RequiresParameterValue("id", @"^2$")]
[ActionName("AjaxAction")]
public ActionResult AjaxAction1(string id) { ... }

[RequiresParameterValue("id", @"^[^2]*$")]
[ActionName("AjaxAction")]
public ActionResult AjaxAction2(string id) { ... }

正如您从本示例中看到的,自定义操作方法选择器采用两个参数:

  • 控制器操作参数名称(示例中的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:

[RequiresParameterValue("id", @"^2$")]
[ActionName("AjaxAction")]
public ActionResult AjaxAction1(string id) { ... }

[RequiresParameterValue("id", @"^[^2]*$")]
[ActionName("AjaxAction")]
public ActionResult AjaxAction2(string id) { ... }

As you can see from this example the custom action method selector takes two parameters:

  • controller action parameter name (id in example)
  • regular expression that checks controller action parameter value

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 when id != "2".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文