为什么Html.ActionLink呈现"?Length=4"

 2023-02-17    320  

问题描述

我非常困惑为什么这个代码

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

结果在此链接中:

为什么Html.ActionLink呈现"?Length=4"

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

hidefocus部分是我旨在实现的目标,但?Length=4来自哪里?

推荐答案

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

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

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

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

其他推荐答案

我解决的方式是在匿名声明(new {})之前将null添加到第四个参数,以便它使用以下方法过载:( linktext,actionname,controllerrername,routevalues,htmlattributes):

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

其他推荐答案

您忘记添加htmlattributes parm.

这将在没有任何变化的情况下工作:

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

以上所述是小编给大家介绍的为什么Html.ActionLink呈现"?Length=4",希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/34217.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。