Asp.net-Mvc

Razor actionlink 在 URL 中自動生成 ?length=7?

  • December 5, 2010

我在剃須刀頁面上有以下連結:

@Html.ActionLink("Create New Profile", "Create", "Profile", new { @class="toplink" })

它出現在視圖頁面的原始碼中,如下所示:

<a href="/admin/profile/create?length=7" class="toplink">Create New Profile</a>

當我點擊連結時,URL 是這樣的:

http://localhost:54876/admin/profile/create?length=7

我不想?length=7。為什麼會自動生成這個?

您正在使用的ActionLink覆蓋匹配(字元串連結文本、字元串 actionName、對象 routeValues、對象 htmlAttributes)覆蓋。因此,您的“配置文件”值正在傳遞給routeValues參數。此函式關於此參數的行為是獲取其上的所有公共屬性並將其添加到用於生成連結的路由值列表中。由於字元串只有一個公共屬性(長度),因此您最終會得到“長度 = 7”。

您要使用的正確重載是(string linkText, string actionName, string controllerName, Object routeValues, Object htmlAttributes),您稱之為 loke:

@Html.ActionLink("Create New Profile", "Create", "Profile", new {}, new { @class="toplink"})

引用自:https://stackoverflow.com/questions/4357856