Razor actionlink 自动生成 URL 中的 length=7?
我在剃刀页面上有以下链接:
@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
。为什么会自动生成这个?
I have the link below on a razor page:
@Html.ActionLink("Create New Profile", "Create", "Profile", new { @class="toplink" })
It appears in thes source of view page as shown below:
<a href="/admin/profile/create?length=7" class="toplink">Create New Profile</a>
When I click on the link the URL is like this:
http://localhost:54876/admin/profile/create?length=7
I don't want ?length=7
. Why is this auto generated?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用的
ActionLink
覆盖与 (字符串 linkText、字符串 actionName、对象 RouteValues、对象 htmlAttributes) 覆盖。因此,您的“Profile”值将传递给routeValues
参数。该函数相对于此参数的行为是获取其所有公共属性并将其添加到用于生成链接的路由值列表中。由于字符串只有一个公共属性(长度),因此最终会得到“length=7”。您要使用的正确重载是 (string linkText ,字符串actionName,字符串controllerName,对象routeValues,对象htmlAttributes),你这样称呼它:
The
ActionLink
override you are using matches to the (string linkText, string actionName, Object routeValues, Object htmlAttributes) override. So your "Profile" value is being passed to therouteValues
parameter. The behavior of this function with respect to this parameter is to take all public properties on it and add it to the list of route values used to generate the link. Since a String only has one public property (Length) you end up with "length=7".The correct overload you want to use is the (string linkText, string actionName, string controllerName, Object routeValues, Object htmlAttributes) and you call it loke so:
我不确定其确切原因,但将其更改为:
当您省略最后一个参数(
htmlattributes
是添加的参数)时,我不知道 MVC 会选择哪个重载,但是会修复它。有一天我会调查并弄清楚究竟发生了什么。I'm not sure the exact cause of this, but change it to:
I don't know which overload MVC is picking when you leave off the last parameter (
htmlattributes
is the added one), but that will fix it. One of these days I'll investigate and figure out exactly what's going on.另一件需要注意的事情是,由于您是在 @ActionLink 中定义控制器,因此您可能不需要这样做,例如,您的“创建新配置文件”
@ActionLink
的视图code> 的表达形式可能是“/admin/profile/index.cshtml”,这是一个列出现有配置文件的视图,在这种情况下,您不需要在@ActionLink
中将控制器定义为@ActionLink
已经相对于ProfileController
,因此您的@ActionLink
可能是我使用
null
而不是>new{}
正如标记的答案一样,我认为这对我自己来说更合适。 ActionLink 重载并不是最简单的事情。Another thing to note, since you are defining the controller in the
@ActionLink
, which you may not need to do, for example, the view that your "Create New Profile"@ActionLink
is expressed in might be "/admin/profile/index.cshtml", a view that lists existing profiles, in this case, you do not need to define the controller in the@ActionLink
as the@ActionLink
is already relative to theProfileController
, so your@ActionLink
could beI used
null
instead ofnew{}
as the marked answer does, I think this is more appropriate myself. ActionLink overloads are not the most straightforward thing ever.