我有一个MVC4应用程序,我在RouteConfig中定义的路由工作正常,如下所示:
routes.MapRoute(
"LocalizedDefault", "{lang}/{homePage}",
new { controller = "Home", action = "Index", pageCode = "home" },
new {lang = @"(en|es)", homePage = @"(home|inicio)"}
);
routes.MapRoute(
"Flowers",
"{lang}/{page}",
new {controller = "Plants", action = "Flowers", pageCode = "flowers"},
new {lang = @"(en|es)", page= @"(flowers|flores)"}
);
/* ... more routes ... */
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
因为你可以看到每个路由都有一个我需要更改语言的参数(我的业务逻辑),问题是当我把url放在没有http://www.mydomain.com之类的任何参数时,它会加载我的默认页面但是我不能改变语言,因为我没有我的页面参数,然后我改变我的路线:
routes.MapRoute(
"Flowers",
"{lang}/{page}",
new {controller = "Plants", action = "Flowers", pageCode = "flowers"},
new {lang = @"(en|es)", page= @"(flowers|flores)"}
);
/* ... more routes ... */
routes.MapRoute(
"Default",
"{lang}/{page}",
new { controller = "Home", action = "Index", pageCode = "home" },
new {lang = @"(en|es)", page = @"(home|inicio)"}
);
现在我的网站在调用http://www.mydomain.com时改变了语言,但其他路线不再有效(例如:http://www.mydomain.com/en/flowers)
答案 0 :(得分:0)
尝试使用默认路由并将参数添加到您的网址中,就像这样...
http://mydomain.com/?lang=es
然后在您的家庭控制器上进行索引操作......
public ActionResult Index(String? lang){
//do some stuff when lang != null
return View();
}