0

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

fellows
  • 3
  • 4
  • This makes me question your design. What is the purpose of the "back" route? – gudatcomputers Mar 21 '14 at 03:15
  • saperate between desktop and mobile site. my client need desktop URL site look like abc.com/controller/action but abc.com/back/controller/action for mobile – fellows Mar 21 '14 at 03:54
  • have you looked at using a responsive framework like http://foundation.zurb.com/ or http://getbootstrap.com/? You wouldn't need this odd specialized routing and might be a better experience for your clients. Especially with deep linking from search results. – gudatcomputers Mar 25 '14 at 02:18
  • @mckeejm ; Thanks for information. I'll research on them. – fellows Mar 25 '14 at 04:59
  • np, not trying to derail the thread or avert your real question. Hope those will be useful for you in the future. – gudatcomputers Mar 26 '14 at 02:57

1 Answers1

0

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() }));
}
Oleksii Aza
  • 5,368
  • 28
  • 35