I have the following route table:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "UserRoute",
url: "{username}",
defaults: new { controller = "User", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
so, when we have url like : http://mysite/abcde it calls UserController, action Index, if we have url like : http://mysite/dashboard/Index it calls DashboardController, action Index. Ok. But when I try to call the following:
return RedirectToAction("Index", "Dashboard");
It calls UserController, action Index with username parameter equals "Dashboard". Why and how to solve?
UserRoute
matches anything with only one segment (which yourRedirectToAction()
generates because the default action isIndex
) – user3559349userRoute
?. By default all controllers redirect to the index action if no action is passed... correct? – David Espino{username}
section of your first route if, and only if a user exists of that name. Even then, you'd still be in trouble if a user decided to call themselvesHome
... The core of this problem is having such a greedy route forUserRoute
. PerhapsUser/{username}
might be a more workable route that doesn't tread on the toes of your second route? – spender