I am having a problem when trying to use RedirectToAction()
.
In my RouteConfig.cs
I have something like this:
routes.MapRoute(
name: "Custom",
url: "myroot/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = "0" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = "0" }
);
From my controller, if I try this:
public ActionResult Contact()
{
return RedirectToAction("About", "Home");
}
I get this URL:
http://localhost:51547/myroot/Home/About
Which is not what I expected. If I try this:
return RedirectToRoute("Default", new { controller = "Home", action = "About" });
I get this URL:
http://localhost:51547/Home/About
Which makes sense to me.
Can anybody explain why RedirectToAction
is adding the myroot/
prefix to the URL instead of matching the Default
route?
Thank you!
Please change your Route order as
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = "0" }
);routes.MapRoute(
name: "Custom",
url: "myroot/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = "0" }
);