RedirectToAction()(MVC5)

时间:2015-09-27 14:00:23

标签: c# asp.net-mvc-5 redirecttoaction

尝试使用RedirectToAction()时遇到问题。

在我的RouteConfig.cs我有这样的事情:

        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" }
        );

从我的控制器,如果我尝试这个:

    public ActionResult Contact()
    {
        return RedirectToAction("About", "Home");
    }

我收到了这个网址:

http://localhost:51547/myroot/Home/About

这不是我的预期。如果我试试这个:

return RedirectToRoute("Default", new { controller = "Home", action = "About" });

我收到了这个网址:

http://localhost:51547/Home/About

这对我来说很有意义。

有人可以解释为什么RedirectToAction正在为网址添加myroot/前缀而不是匹配Default路由吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

请将您的路线订单更改为

    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" }
    );

答案 1 :(得分:0)

这可能有用......未经过测试

return RedirectToAction("action", "controller", new { area = "" });