我有一堆路由定义为:
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" })
请求转到home contoller index函数.我做错了什么.如何使用routeLink以便请求应该转到默认路由.
最佳答案 我也有这个问题.
似乎代码通过签名调用RouteLink重载
RouteLink(string linkText, object routeValues, object htmlAttributes)
这意味着routeName永远不会使用此重载进行设置,并且很可能您获得具有空href属性的锚元素或使用默认控制器和操作生成的链接,因为routeValues也未设置为您期望的值.
我们想要实际调用此重载
RouteLink(string linkText, string routeName, object routeValues)
这可以通过使用如下命名参数来确保:
@Html.RouteLink("Create Ad", routeName: "Default", routeValues: new { controller="Electronics",action="Details" })