我有一堆路线定义为:
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" })
该请求转到家用 Controller 索引功能。我做错了。如何使用routeLink,以便请求应转到默认路由。
最佳答案
我也有这个问题。
似乎代码调用带签名的RouteLink重载
RouteLink(string linkText, object routeValues, object htmlAttributes)
这意味着routeName永远不会设置有此重载,并且很可能会获得带有空href属性的 anchor 元素或使用默认 Controller 和操作生成的链接,因为routeValues也未设置为您期望的值。
我们实际上要称呼这个重载
RouteLink(string linkText, string routeName, object routeValues)
可以通过使用如下命名参数来确保:
@Html.RouteLink("Create Ad", routeName: "Default", routeValues: new { controller="Electronics",action="Details" })
关于asp.net-mvc - html.routelink没有转到指定的路线,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32325937/