簡體   English   中英

html.routelink不會轉到指定的路由

[英]html.routelink does not go to specified route

我有一堆路由定義為:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM