1. 程式人生 > >每日踩坑 2018-11-26 MVC Razor ActionLink 生成的URL中多生成了一個引數 ?length=n

每日踩坑 2018-11-26 MVC Razor ActionLink 生成的URL中多生成了一個引數 ?length=n

RouteConfig 的路由註冊如下:

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

Razor 程式碼:

@Html.ActionLink(" 主頁", "Index", "Home", new { @class = "fa fa-dashboard
" })

這個時候 單擊F12 其命中的方法簽名是

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes);

可以看到本該三個引數方法簽名中的 controllerName 變成了 object routeValues

這時只需將頁面 Razor 程式碼改為:

@Html.ActionLink(" 主頁", "Index", new
{ controller = "Home" }, new { @class = "fa fa-dashboard" })

即可。