문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top