2015-09-01 98 views
4

我定義爲路線的一串:html.routelink不走指定路線

routes.MapRoute(
       name: "ID", 
       url: "{category}/{subcategory}/{lowercategory}/{lowercategory1}/{id}/{ignore}", 
       defaults: new { controller = "Home", action = "Index", ignore = UrlParameter.Optional } 
      ); 

      routes.MapRoute(
       name: "LowerCategory1", 
       url: "{category}/{subcategory}/{lowercategory}/{lowercategory1}", 
       defaults: new { controller = "Home", action = "Index" } 
      ); 

      routes.MapRoute(
       name: "LowerCategory", 
       url: "{category}/{subcategory}/{lowercategory}", 
       defaults: new { controller = "Home", action = "Index" } 
      ); 

      routes.MapRoute(
       name: "Subcategory", 
       url: "{category}/{subcategory}", 
       defaults: new { controller = "Home", action = "Index" } 
      ); 

      routes.MapRoute(
       name: "Category", 
       url: "{category}", 
       defaults: new { controller = "Home", action = "Index" } 
      ); 

      routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      ); 

現在我用routeLink訪問缺省路由,而是它不工作。

@Html.RouteLink("Create Ad", "Default", new { controller="Electronics",action="Details" }) 

該請求會轉到家庭控制器索引功能。我做錯了什麼。如何使用routeLink,以便請求應該轉到默認路由。

回答

2

我也有這個問題。 看來,代碼調用與簽名

RouteLink(string linkText, object routeValues, object htmlAttributes) 

的RouteLink超載這意味着routeName永遠不會設置這種超載,最有可能你用默認的控制器和產生的空href屬性或鏈接的錨元素行動,因爲routeValues也沒有設置爲你所期望的。

我們要真正把這個過載,而不是

RouteLink(string linkText, string routeName, object routeValues) 

可以通過使用命名參數,像這樣保證:

@Html.RouteLink("Create Ad", routeName: "Default", routeValues: new { controller="Electronics",action="Details" })