Razor actionlink 自动生成 URL 中的 length=7?

发布于 2024-10-06 06:14:16 字数 431 浏览 2 评论 0原文

我在剃刀页面上有以下链接:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小耗子 2024-10-13 06:14:16

您使用的 ActionLink 覆盖与 (字符串 linkText、字符串 actionName、对象 RouteValues、对象 htmlAttributes) 覆盖。因此,您的“Profile”值将传递给 routeValues 参数。该函数相对于此参数的行为是获取其所有公共属性并将其添加到用于生成链接的路由值列表中。由于字符串只有一个公共属性(长度),因此最终会得到“length=7”。

您要使用的正确重载是 (string linkText ,字符串actionName,字符串controllerName,对象routeValues,对象htmlAttributes),你这样称呼它:

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

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 the routeValues 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:

@Html.ActionLink("Create New Profile", "Create", "Profile", new {}, new { @class="toplink"})
爱*していゐ 2024-10-13 06:14:16

我不确定其确切原因,但将其更改为:

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

当您省略最后一个参数(htmlattributes 是添加的参数)时,我不知道 MVC 会选择哪个重载,但是会修复它。有一天我会调查并弄清楚究竟发生了什么。

I'm not sure the exact cause of this, but change it to:

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

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.

山人契 2024-10-13 06:14:16

另一件需要注意的事情是,由于您是在 @ActionLink 中定义控制器,因此您可能不需要这样做,例如,您的“创建新配置文件”@ActionLink 的视图code> 的表达形式可能是“/admin/profile/index.cshtml”,这是一个列出现有配置文件的视图,在这种情况下,您不需要在 @ActionLink 中将控制器定义为@ActionLink 已经相对于 ProfileController,因此您的 @ActionLink 可能是

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

我使用 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 the ProfileController, so your @ActionLink could be

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

I used null instead of new{} as the marked answer does, I think this is more appropriate myself. ActionLink overloads are not the most straightforward thing ever.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文