Why does Html.ActionLink render “?Length=4”

2019-01-01 04:52发布

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 hidefocus part is what I was aiming to achieve, but where does the ?Length=4 come from?

标签: asp.net-mvc
10条回答
谁念西风独自凉
2楼-- · 2019-01-01 05:08

You forgot to add the HTMLAttributes parm.

This will work without any changes:

Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" },null)
查看更多
美炸的是我
3楼-- · 2019-01-01 05:11

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

Html.ActionLink("About", "About", new { hidefocus = "hidefocus" })
查看更多
只若初见
4楼-- · 2019-01-01 05:13

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

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

This takes a string object "Home" for routeValues, which the MVC plumbing searches for public properties turning them into route values. In the case of a string object, 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 HomeController it will throw an error about a missing About action method. Try using the following:

Html.ActionLink("About", "About", new { controller = "Home" }, new { hidefocus = "hidefocus" })
查看更多
零度萤火
5楼-- · 2019-01-01 05:13

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

@using (@Ajax.BeginForm("Register", "Account", null,
    new AjaxOptions
    {
        HttpMethod = "POST",
        OnSuccess = "OnSuccess",
        OnFailure = "OnFailure",
        OnBegin = "OnBegin",
        OnComplete = "OnComplete"
    }, new { @class = "form-login" }))
查看更多
唯独是你
6楼-- · 2019-01-01 05:18

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

Html.BeginForm()

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

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:

using (Html.BeginForm()) {
      @Html.AntiForgeryToken()
      ...
    }
查看更多
何处买醉
7楼-- · 2019-01-01 05:20

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

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

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

查看更多
登录 后发表回答