我希望用户可以使用
访问我的网站English version: web.com/news/bla-bla
Spanish version: web.com/noticias/bla-asdf
...
我认为修改RouteConfig.cs是解决方案,但我不知道它是怎么做的。我接受的解决方案不是MapRoute。
我的第一次测试失败:
namespace MyWebsite
{
public class RouteConfig
{
routes.MapRoute(
name: "Default",
url: {id},
defaults:
// In a web running in a server I should use local resources
if ({id} == ("about" || "sobre" || "sur"))
{
defaults: new { controller = "Home", action = "about"}
}
}
}
}
我的第二次失败测试: 错误:(附加信息:名为'关于'的路线已经在路线集合中。路线名称必须是唯一的。) - >我无法重新接触"路线。
string[] about = { "about", "sobre", "sur" };
for (int i = 0; i < about.Length; i++)
{
//if ({id}.Equals(about[i])
routes.MapRoute(
name: "About",
url: about[i],
defaults: new { controller = "Home", action = "About"}
);
答案 0 :(得分:0)
您必须通过将动态语言值传递给控制器来单独映射每个路由。例如:
routes.MapRoute(
name: "News English",
url: "News",
defaults: new { controller = "Home", action = "News", language = "en-US"}
);
routes.MapRoute(
name: "News Italian",
url: "Notizie",
defaults: new { controller = "Home", action = "News", language = "it-IT" }
);
并且在您的控制器中,您可以获得语言并使用它执行您想要的操作(例如,设置当前文化和当前UI文化):
public ActionResult News(string language)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(language);
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(language);
//Do what you want
return View();
}