asp.net-mvc Razor actionlink 自动生成 ?length=7 在 URL 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4357856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Razor actionlink autogenerating ?length=7 in URL?
提问by Pirzada
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?
我不要?length=7
。为什么会自动生成这个?
回答by marcind
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".
ActionLink
您使用的覆盖与(字符串链接文本、字符串操作名称、对象路由值、对象 htmlAttributes)覆盖相匹配。所以你的“配置文件”值被传递给routeValues
参数。此函数相对于该参数的行为是获取其上的所有公共属性并将其添加到用于生成链接的路由值列表中。由于 String 只有一个公共属性 (Length),因此您最终会得到“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:
您要使用的正确重载是(string linkText, string actionName, string controllerName, Object routeValues, Object htmlAttributes)并且您将其称为loke:
@Html.ActionLink("Create New Profile", "Create", "Profile", new {}, new { @class="toplink"})
回答by Matt Sieker
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.
我不知道当您离开最后一个参数(htmlattributes
是添加的参数)时,MVC 正在选择哪个重载,但这会解决它。这几天我会调查并弄清楚到底发生了什么。
回答by Brian Ogden
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
另一件要注意的事情是,由于您在 中定义控制器@ActionLink
,您可能不需要这样做,例如,表示您的“创建新配置文件”的视图@ActionLink
可能是“/admin/profile/index.cshtml”,列出现有配置文件的视图,在这种情况下,您不需要在 中定义控制器,@ActionLink
因为@ActionLink
已经相对于ProfileController
,因此您@ActionLink
可以
@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.
我使用null
而不是new{}
像标记的答案那样,我认为这更适合我自己。ActionLink 重载并不是最简单的事情。