Search code examples
c#asp.net-mvcasp.net-mvc-5t4mvccustom-action-filter

ActionExecutingContext.ActionParameters NULL on RedirectToAction MVC 5 - T4MVC


I have a redirection like:

return RedirectToAction(MVC.Area.Controller.Index(institutionId));

This redirects to a controller which inherits from another controller which has an action filter defined like so:

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    var institutionId = filterContext.ActionParameters["institutionId"];

    var institution = InstitutionRepository.GetById((int)institutionId);

    ...other code
}

the ActionExecutingContext.ActionParameters contain the institutionId but the value is always NULL. I've tried redirecting to route as well but that gives the same output. What bothers me is that when I look at the route, the institutionId seems to be passed with query parameters (Area/Controller?institutionId=17 instead of Area/Controller/17)

MVC stands for T4MVC, which is framework used to avoid hardcoding strings in the redirects.


Solution

  • If anyone at some point might encounter it, it's actually quite tricky. It seems in some cases T4MVC confuses views and interprets:

    return RedirectToAction(MVC.Area.Controller.Index(institutionId));
    

    as

    return RedirectToAction(MVC.Area.OtherController.Index(institutionId));
    

    Looking at the ActionParameters, the collection had both Controller and OtherController in the routeData so it seems it just looked up the Controller name by the View name (Index), found two instances belonging to both Controllers and generated a broken MVC redirection string. After I changed the name of the View, the ActionParameters started resolving correctly.