我在 Razor 页面上有以下链接:
@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
覆盖与 (string linkText, string actionName, Object routeValues, Object htmlAttributes) 匹配覆盖。因此,您的“Profile”值将传递给 routeValues
参数。该函数相对于此参数的行为是获取其所有公共(public)属性并将其添加到用于生成链接的路由值列表中。由于字符串只有一个公共(public)属性(长度),因此最终会得到“length=7”。
您要使用的正确重载是 (string linkText, string actionName, string controllerName, Object routeValues, Object htmlAttributes)你这样调用它:
@Html.ActionLink("Create New Profile", "Create", "Profile", new {}, new { @class="toplink"})
关于asp.net-mvc - Razor actionlink 自动生成 URL 中的 length=7?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4357856/