2013-05-15 72 views
2

我想知道实现此目的的最佳方法是什么。Mvc:具有不同名称的相同ActionResult取决于url

我在我的控制器中的ActionResult,实际上却news的名字,现在我需要internationlize我的网站,我不能有相同的news名字,它必须根据在那里的访问了该国改变。

例如,现在我需要类似的东西。

www.something.com/en/us/news英文版本

www.something.com/co/es/noticias西班牙语版本

你的下一个国家的地步。

我不认为我需要创建X方法根据X网址,使完全一样的,但我不知道如何实现它的一个非常有效的方式...感谢

回答

0

你的路由现在如何工作?如果你还没有使用它,可能会像answer那样工作。也许这是一个变化,以不同顺序的URL部分,以满足您的需求。例如,控制器不一定必须首先进入路径(或者根本就不必使用相同的控制器名称)。制作某种地图,使用语言代码作为关键字,为每个不同语言提供“新闻”一词。

// populate this map somewhere - language code to word for "news" (and any other name of the controllers that you have) 
var newsControllerMap = new Dictionary<string, string>(); 
newsControllerMap["en"] = "news"; // etc. 

// ... 

// inside of the RouteConfig class (MVC 4) or RegisterRoutes() method in Global.asax.cs (MVC 3) 
// just making an assumption that whatever class/entity you use ("LanguageAndCountry" in this case) also has a country code to make this easier. Obviously this would be refactored to have better naming/functionality to make sense and meet your needs. 
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
    LanguageAndCountryRepository langRepo = new LanguageAndCountryRepository(); 
    var languagesandCountries = langRepo.GetAllLanguagesWithCountries(); 

    foreach (LanguageAndCountry langAndCountry in languagesandCountries) 
    { 
     routes.MapRoute(
     "LocalizationNews_" + langAndCountry.LanguageAbbreviation, 
     langAndCountry.LanguageAbbreviation + "/" + langAndCountry.CountryCode + "/" + newsControllerMap[langAndCountry.LanguageAbbreviation], 
     new { lang = language.LanguageAbbreviation, country = langAndCountry.CountryCode, controller = "News", action = "Index"}); 

     // map more routes to each controller you have, each controller having a corresponding map to the name of the controller in any given language 
    }