Pergunta

I googled a lot but still got no luck.

This is my RouteConfig.cs.

routes.MapRoute("BackRoute", "back/{controller}/{action}"
     ,new { controller = "Home", action = "Index" });

routes.MapRoute("Default", "{controller}/{action}"
     ,new { controller = "Home", action = "Index" });

As you see,There is the same parameter but I've business logic to choose between BackRoute and Default from code behind.

Can I change route from ActionFilterAttribute ?

public override void OnActionExecuting(ActionExecutingContext filterContext){
   bool logic = true;
   RouteValueDictionary rvd = filterContext.RouteData.Values;
   if(logic){
       filterContext = new RedirectToRouteResult("BackRoute", 
                       new RouteValueDictionary(new { 
                             controller = rvd["controller"].ToString() 
                           , action = rvd["action"].ToString() }));
   }
}

Can you guys suggest me the good way to achieve this goal ?

Thank you in advance,

Peace

Foi útil?

Solução

In your attribute you can use:

filterContext.Result = new RedirectToRouteResult("BackRoute",
     new RouteValueDictionary(
               new { action = "Index", controller = "Home" });

UPDATE: Infinite loop means that you are redirecting to action called in first place. Try to add one more check before setting RedirectToRouteResult:

if(logic && filterContext.RouteData.Route != RouteTable.Routes["BackRoute"])
{
       filterContext.Result = new RedirectToRouteResult("BackRoute", 
                                new RouteValueDictionary(new { 
                                     controller = rvd["controller"].ToString() 
                                     , action = rvd["action"].ToString() }));
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top