Html.ActionLink render “?Length=数值”

来源:互联网 发布:遇到网络骗局怎么办 编辑:程序博客网 时间:2024/05/22 18:06

分享一下遇到这个问题原因:

译文:

我对这段代码的结果非常困惑:

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

运行结果:

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

我要实现的是:hidefocus,但是为什么会出现下面这一部分:

"?Length=4"

解释:

这个”?Length=4”来自于尝试序列化字符串对象的结果,你的代码实际调用的是下面的方法:

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

在此实际提供的控制器是Home,而MVC管道实际上是搜索他们路由值routeValue。
在传入字符串对象的情况下,只有一个公共属性就是length,所以会有一个长度参数作为它附加的属性名称和值传递到一个没有定义的路由值中。你可能会发现你运行的这个action并不在HomeController中,由于about action的缺失,导致了这个错误。请尝试以下代码:

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

在学习中我们可能都遇到过这种问题,在此与大家分享,外文水平有限,译文有错误或不准确的地方请大家批评。

原文:

http://stackoverflow.com/questions/824279/why-does-html-actionlink-render-length-4

0 0