htmlHelper.RouteLink没有生成我想要的URL

时间:2012-04-13 12:13:23

标签: asp.net-mvc-3 localization routing

您好我正在尝试利用great article在我的mvc3网站上提供本地化功能。没有区域工作完美但在我的网站中引入区域我遇到了这个问题:

htmlHelper.RouteLink(linkText, globalisedRouteData)

其中globalisedRouteData:

4 Keys: Culture, Area, Controller, Action
4 values: en, soluciones, home, index

没有产生预期的(对我来说):host / en / soluciones / home / Index 但是:/ soluciones / Home?culture = en

这让我感到困惑的是什么?culture = en。为什么没有嵌入/ en /,因为它包含在globalisedRouteData中?

在我的RegisterRoutes中,我放置了:

        const string defautlRouteUrl = "{area}/{controller}/{action}/{id}";
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        RouteValueDictionary defaultRouteValueDictionary = new RouteValueDictionary(new {area="soluciones_de_salud",   controller = "Home", action = "Index", id = UrlParameter.Optional });
        routes.Add("DefaultGlobalised",  new GlobalisedRoute(defautlRouteUrl, defaultRouteValueDictionary));

Thanks¡¡

2 个答案:

答案 0 :(得分:0)

我相信您需要将'id'的使用更改为'culture'

const string defautlRouteUrl = "{area}/{controller}/{action}/{culture}";

RouteValueDictionary defaultRouteValueDictionary = new RouteValueDictionary(new {area="soluciones_de_salud",   controller = "Home", action = "Index", culture = UrlParameter.Optional })

当路由绑定发生时,参数名称必须与route参数匹配,否则最终会以

结束
?ParamName=Value      (?culture=en)

答案 1 :(得分:0)

尝试更改id

中的culture

如果您的网址中的文化是强制性的,请同时删除id = UrlParameter.Optional

const string defautlRouteUrl = "{area}/{controller}/{action}/{culture}";
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
RouteValueDictionary defaultRouteValueDictionary = new RouteValueDictionary(new {area="soluciones_de_salud",   controller = "Home", action = "Index" });
routes.Add("DefaultGlobalised",  new GlobalisedRoute(defautlRouteUrl, defaultRouteValueDictionary));

如果文化不是强制性的,您可以设置默认值

const string defautlRouteUrl = "{area}/{controller}/{action}/{culture}";
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
RouteValueDictionary defaultRouteValueDictionary = new RouteValueDictionary(new {area="soluciones_de_salud",   controller = "Home", action = "Index", culture = "en" });
routes.Add("DefaultGlobalised",  new GlobalisedRoute(defautlRouteUrl, defaultRouteValueDictionary));