ASP MVC, how to get controller which called RedirectToAction

340 views Asked by At

I have 2 controllers.

public class HomeController : Controller
    {
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                // do some stuff if controller before was ServiceController
                    base.OnActionExecuting(filterContext);           

            }

    public ActionResult Index()
            {            
                    return View();
            }
}

and ServiceController with

public ActionResult Confirm()
            { return RedirectToAction("Index", "Home");}

How can I know in HomeController if action was called by RedirectToAction from Service controller? Is there any way? (I don't want to use cookies or send it in parameters, because I have lots of RedirectToAction in whole controller and I want to apply it globally for controller)

1

There are 1 answers

4
Brad C On

RedirectToAction is actually part of the controller itself and not an actionresult so you could override it and populate the tempdata you need from there:

protected override RedirectToRouteResult RedirectToAction(string actionName, string controllerName, System.Web.Routing.RouteValueDictionary routeValues)
{
    TempData["RedirectedFromAction"] = this.ControllerContext.RouteData.Values["action"].ToString();
    TempData["RedirectedFromController"] = this.ControllerContext.RouteData.Values["controller"].ToString();
    return base.RedirectToAction(actionName, controllerName, routeValues);
}

Then you can check filterContext.Controller.TempData["RedirectedFromController"] is ServiceController or whatever. If you need this in more than one spot, you can create a BaseController.