سؤال

I'm looking to get the action that caused OnActionExecuting() in an overridden ActionFilterAttribute. The idea is to save this in the session so the user can be redirected to their intended page after entering an interim "Change Password" page.

My current attempt is:

public override void OnActionExecuting(ActionExecutingContext actionExecutingContext)
{
    // ...
    ActionResult originatingActionResult = new RedirectToRouteResult(
        new RouteValueDictionary
        {
            { "controller", actionExecutingContext.ActionDescriptor.ControllerDescriptor.ControllerName },
            { "action", actionExecutingContext.ActionDescriptor.ActionName }
        });
    // ...

But this seems rather complex for what is being done - is there a simpler way?

هل كانت مفيدة؟

المحلول

Turned out it could be simplified as:

ActionResult originatingActionResult = new RedirectToRouteResult(
        new RouteValueDictionary(filterContext.RouteData.Values));

However...

The project uses T4MVC and RedirectToAction couldn't be used as it is overridden on the controller and expects GetT4MVCResult() to be present on the ActionResult.

So I ended up doing it like this instead:

RouteValueDictionary originatingRouteValues = new RouteValueDictionary(
    actionExecutingContext.RouteData.Values);

// ... followed by ...

return this.RedirectToRoute(routeValues);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top