html.routelink不会转到指定的路由

时间:2015-09-01 07:29:26

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

我有一堆路由定义为:

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使请求进入默认路由。

1 个答案:

答案 0 :(得分:2)

我也有这个问题。 似乎代码使用签名

调用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" })