如前所述,這與網站網址(和路線)使用英語的慣例不同。
儘管如此,但爲了做到這一點,您可能必須考慮爲每種外語生成一條路徑。因此,對於有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);
}
我會LOVE,如果有人走過來,說,有這樣做的更好的辦法,因爲我同意擁有外語網址並不好,並且考慮到互聯網本身正在朝着允許非羅馬字符的URL發展,我們越早尋求解決方案就越好。
不僅如此,但我知道驕傲的法國人不喜歡看到他們的網站URL包含英語。 :)
其實這不是一個好主意,如果你關心搜索引擎的排名。您總是可以重定向到英文頁面,或者對同一實體的所有實例使用標準規範URL。 – 2011-02-02 21:20:28