2016-07-04 145 views
0

我有一个问题,似乎无法解决它。我有一个网址,像多数民众赞成:将查询字符串转换为路由路径

myurl/?culture=fr 

我要的是

myurl/fr 

我的控制器看起来像:

public ActionResult Index(string culture = null) 

和我routeConfig:

routes.MapRoute(
    name: "Languages", 
    url: "{controller}/{action}/{culture}" 
); 

这导致页面无法正确重定向。 任何提示解决它?

+0

你能否提供更多的细节?当您尝试使用myurl/fr语法进行重定向时,您看到了什么结果? –

+0

我收到:'/'应用程序中的服务器错误。 找不到资源。 –

+0

[ASP.NET MVC 5在路由和URL中的文化]的可能重复(http://stackoverflow.com/q/32764989/181087) – NightOwl888

回答

2

如果您将“fr”附加到URL的根目录(www.yourUrl.com/fr),则在路由中给予默认的Controller和动作。像这样: -

routes.MapRoute(
name: "Languages", 
url: "{controller}/{action}/{culture}", 
defaults: new { controller = "Home", action = "Index"} 

);

使用默认控制器替换“Home”,使用默认操作替换“Index”。

+0

你能解释一下吗,我的routes.MapRoute应该是什么? –

+0

编辑我的回答 – CuriousGeek

+0

它不起作用。如果myurl/fr –