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" }
);
这可能有效...未经测试
return RedirectToAction("action", "controller", new { area = "" });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.