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)
开发者_如何学JAVA{
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.
Found the answer you can subclass the Controller and create the ControllerActionInvoker there.
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"
.
精彩评论