MVC Response.RedirectToRoute(RouteData.Values)重定向到Area控制器

时间:2014-05-17 13:34:41

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing

我使用RedirectToRoute方法强制文化在网址中。一切正常,直到我在项目中创建一个Admin区域。现在该方法重定向到区域控制器。

主HomeController示例:/ Home / Contact

public ActionResult Contact()
    {
       Response.RedirectToRoute(RouteData.Values);
       return View();
    }

该方法重定向到/ Admin / Home / Contact,重定向前RouteData.Values的值为:

[0] "Controller"  Home
[1] " Action"     Contact

我的主要路线:

routes.MapRoute("Default_culture", "{culture}/{controller}/{action}/{id}",
                            new { controller = "Home", action = "Index",  id = UrlParameter.Optional , new { culture = "([a-z]{2,3})(-[a-zA-Z]{2})?" }, new[] { "Project.Controllers" });

routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "Project.Controllers" });

在RegisterArea方法中路由:

context.MapRoute(null, "{culture}/Admin/{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new { culture = "([a-z]{2,3})(-[a-zA-Z]{2})?" }, new[] { "Project.Areas.Admin.Controllers" });

context.MapRoute(null, "Admin/{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "Project.Areas.Admin.Controllers" });

我不明白这种行为。我做错了什么?

1 个答案:

答案 0 :(得分:2)

我刚刚在我的网站上解决了这个问题。

更改Response.RedirectToRoute(RouteData.Values);

Response.RedirectToRoute("Default", RouteData.Values);