正如之前所建议的,这确实偏离了网站网址(和路线)使用英文的惯例。
尽管如此,这是可能的,但为了做到这一点,您可能必须考虑为每种外语的每个操作生成一条路线。因此,对于具有 20 个操作和三种语言(英语、法语和德语)的网站,您将需要 41 条路线(20 条法语、20 条德语和 1 条英语)。我承认,这不是最有效的系统,但它可以按您的意愿工作。
//You'll only need one of these, which is the default.
routes.MapRoute(
"English route",
"en/{controller}/{action}/{id}"
new { controller = "Home", action = "Index", language = "en" },
);
routes.MapRoute(
"FrenchHome",
"fr/Demarrer/Index/{id}",
new { controller = "Home", action = "Index", language = "fr" }
);
routes.MapRoute(
"GermanHome",
"de/Heim/Index/{id}", //'Heim' is, I believe the correct usage of Home in German.
new { controller = "Home", action = "Index", language = "de" }
);
//Some more routes...
routes.MapRoute(
"FrenchSoftware",
"fr/Produit/Logiciels/{id}",
new { controller = "Product", action = "Software", language = "fr" }
);
routes.MapRoute(
"GermanSoftware",
"de/Produkt/Software/{id}", //In this instance, Software should be the same in German and English.
new { controller = "Product", action = "Software", language = "de" }
);
//And finally, the 404 action.
routes.MapRoute(
"Catchall",
"{language}/{*catchall}",
new { controller = "Home", action = "PageNotFound", language = "en" },
new { language = "^(en|fr|de)$" }
);
//This is for the folks who didn't put a language in their url.
routes.MapRoute(
"Catchall",
"{*catchall}",
new { controller = "Home", action = "PageNotFound", language = "en" }
);
在您的操作中,例如产品/软件...
public ActionResult Software(string language, int id)
{
//This would go off to the DAL and get the content in whatever language you want.
ProductModel model = ProductService.GetSoftware(language, id);
return View(model);
}
如果有人过来说有更好的方法,我会喜欢它,因为我同意使用外语的 url 不好,并且考虑到互联网它本身正在朝着允许在 url 中使用非罗马字符的方向发展,我们越早寻找解决方案越好。
不仅如此,我知道骄傲的法国人不喜欢看到他们的网站网址包含英语。 :)