4

When I attempt to call a specific controller action (method) from within another controller action (method), the attributes that have been applied to the action I'm calling aren't applied/enforced.

For example, if I have a Controller action with some attributes applied like so:

[Authorize]
[HttpPost]
public ActionResult SaveUsers(List<User> users) { .. }

and then I call this action from another Controller action, the attributes aren't executed/enforced.

MVC seems to manage the execution of these ActionFilterAttribute/ActionMethodSelectorAttribute within its default implementation of "ActionInvoker" but unfortunately, when you use the ActionInvoker directly, it doesn't return the result of the action method, rather it simply calls the action and writes directly to the Response stream.

My goal is this:

  1. call a specific action method
  2. have attributes enforced/executed
  3. obtain result from action method

I have managed to do 1 and 3, but not 2.

3
  • 6
    Why would you ever need to call an action on another controller? If you need to do that, you probably need to factor out some common behaviour out to a model. Commented Dec 30, 2012 at 18:01
  • I'd like to see the method where you're trying to do this, just to make sure this isn't a kludge. Commented Dec 30, 2012 at 18:08
  • I have the same issue. My use case is this: In my SPA page the HandleUnknownAction method gets called, since my routing is in Javascript. I call the default method Index().ExecuteResult here. But HandleUnknownAction does not execute any filters that are on the controller. So I need a way to apply the attribute filters for the Index() method. Commented May 1, 2013 at 18:11

1 Answer 1

3

You can try to explicitly go through an ActionInvoker. This is the object that executes your actions normally, but it is called by the MVC pipeline. In your source action, do something like this:

this.ActionInvoker.InvokeAction(this.ControllerContext, "TargetActionName");

This may work, but you introduce the action name as a string, which is not a good thing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.