asp.net-mvc 为什么 Html.ActionLink 渲染 "?Length=4"

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/824279/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:33:52  来源:igfitidea点击:

Why does Html.ActionLink render "?Length=4"

asp.net-mvc

提问by My Alter Ego

I'm VERY confused as to why this code

我很困惑为什么这个代码

Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" })

results in this link:

结果在这个链接:

<a hidefocus="hidefocus" href="/Home/About?Length=4">About</a>

The hidefocuspart is what I was aiming to achieve, but where does the ?Length=4come from?

hidefocus部分是我的目标,但?Length=4从哪里来?

回答by roryf

The Length=4 is coming from an attempt to serialize a string object. Your code is running this ActionLinkmethod:

Length=4 来自尝试序列化字符串对象。您的代码正在运行此ActionLink方法:

public static string ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes)

This takes a stringobject "Home" for routeValues, which the MVC plumbing searches for public properties turning them into route values. In the case of a stringobject, the only public property is Length, and since there will be no routes defined with a Length parameter it appends the property name and value as a query string parameter. You'll probably find if you run this from a page not on HomeControllerit will throw an error about a missing Aboutaction method. Try using the following:

这需要一个string对象“Home”作为 routeValues,MVC 管道搜索公共属性并将它们转换为路由值。在string对象的情况下,唯一的公共属性是Length,并且由于没有使用 Length 参数定义的路由,因此它将属性名称和值附加为查询字符串参数。你可能会发现,如果你从一个不在HomeController它上面的页面运行它会抛出一个关于缺少About操作方法的错误。尝试使用以下方法:

Html.ActionLink("About", "About", new { controller = "Home" }, new { hidefocus = "hidefocus" })

回答by Manuel Castro

The way I solved this is was adding a null to the fourth parameter before the anonymous declaration (new {}) so that it uses the following method overload: (linkText, actionName, controllerName, routeValues, htmlAttributes):

我解决这个问题的方法是在匿名声明 ( new {})之前向第四个参数添加一个空值,以便它使用以下方法重载:(linkText, actionName, controllerName, routeValues, htmlAttributes):

Html.ActionLink("About", "About", "Home", null, new { hidefocus = "hidefocus" })

回答by Jesse Rose

You forgot to add the HTMLAttributes parm.

您忘记添加 HTMLAttributes 参数。

This will work without any changes:

这将无需任何更改即可工作:

Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" },null)

回答by Jesse Rose

The parameters to ActionLink are not correct, it's attempting to use the "Home" value as a route value, instead of the anonymous type.

ActionLink 的参数不正确,它试图使用“Home”值作为路由值,而不是匿名类型。

I believe you just need to add new { }or nullas the last parameter.

我相信您只需要添加new { }null作为最后一个参数。

EDIT: Just re-read the post and realized you'll likely want to specify null as the second last parameter, not the last.

编辑:只需重新阅读帖子并意识到您可能希望将 null 指定为倒数第二个参数,而不是最后一个。

回答by user2254436

Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" }, new { })

This will take the overload: string linkText, string actionName, string controllerName, Object routeValues, Object htmlAttributes

这将采用重载:字符串linkText、字符串actionName、字符串controllerName、Object routeValues、Object htmlAttributes

回答by Omveer singh maurya

Kindly use right overloaded method with five (5) parameters. Example:

请使用带有五 (5) 个参数的正确重载方法。例子:

@using (@Ajax.BeginForm("Register", "Account", null,
    new AjaxOptions
    {
        HttpMethod = "POST",
        OnSuccess = "OnSuccess",
        OnFailure = "OnFailure",
        OnBegin = "OnBegin",
        OnComplete = "OnComplete"
    }, new { @class = "form-login" }))

回答by Fil

Just remove "Home" (name of the controller) so that the code would be:

只需删除“Home”(控制器名称),代码将是:

Html.ActionLink("About", "About", new { hidefocus = "hidefocus" })

回答by usefulBee

With attribute names:

使用属性名称:

 @Html.ActionLink(linkText: "SomeText", actionName: "SomeAction", controllerName: "SomeControllerName", routeValues: new { parameterName = parameterValue}, htmlAttributes: null)

回答by Frederik Struck-Sch?ning

As Jonathon Watney pointed out in a comment, this also goes for

正如乔纳森沃特尼在评论中指出的那样,这也适用于

Html.BeginForm()

Html.BeginForm()

methods. In my case, I was in a Create.cshtmltargeting the post request of the corresponding controller + Create action and had

方法。就我而言,我在一个Create.cshtml 中,针对相应控制器 + 创建操作的 post 请求,并且有

using (Html.BeginForm("Create")) {
  @Html.AntiForgeryToken()
  ...
}

which was adding the querystring "?Length=6"to the form action when rendered. Hinted by roryf's approved answer and realizing the string length of "Create" is 6, I finally solved this by removing the explicit action specification:

它在呈现时将查询字符串“?Length=6”添加到表单操作中。由 roryf 批准的答案暗示并意识到“创建”的字符串长度为 6,我终于通过删除显式操作规范解决了这个问题:

using (Html.BeginForm()) {
      @Html.AntiForgeryToken()
      ...
    }

回答by Taersious

Perhaps others had the same issue and need to supply a classvalue via HTMLAttributesparm. Here's my solution:

也许其他人也有同样的问题,需要通过HTMLAttributesparm提供一个值。这是我的解决方案:

@Html.ActionLink("About", "About", new { controller = "Home", area = "" }, new { hidefocus = "hidefocus", @class = "nav-item nav-link" })