Asp.net Net MVC ActionLink

Asp.net Net MVC ActionLink,asp.net,asp.net-mvc,asp.net-mvc-2,extension-methods,Asp.net,Asp.net Mvc,Asp.net Mvc 2,Extension Methods,有人能解释为什么会发生以下情况吗?以及如何解决,Visual Studio 2010和MVC2 <%= Html.ActionLink("Add New Option", "AddOption", "Product", new { @class = "lighbox" }, null)%> 导致 /产品/添加选项?类别=灯箱 导致 /产品/添加选项?长度=7 谢谢您正在使用这些各自的重载: public static MvcHtmlString ActionLink( th

有人能解释为什么会发生以下情况吗?以及如何解决,Visual Studio 2010和MVC2

<%= Html.ActionLink("Add New Option", "AddOption", "Product", new { @class = "lighbox" }, null)%>

导致

/产品/添加选项?类别=灯箱


导致

/产品/添加选项?长度=7


谢谢

您正在使用这些各自的重载:

public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
Object routeValues,
Object htmlAttributes
)
发件人:

发件人:

当第一个
new{@class=“lighbox”}
应该是
htmlAttributes
参数时,它将作为
routeValues
参数传递

这种问题在MVC中使用的扩展方法中很常见。In有时有助于使用命名参数(C#4.0),使内容更具可读性:

<%= Html.ActionLink(linkText: "Add New Option", 
   actionName: "AddOption",
   controllerName: "Product", 
   htmlAttributes: new { @class = "lighbox" }, 
   routeValues: null)%>

这是ASP.NET MVC中“重载地狱”的一个示例

第一个代码调用以下方法:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    string controllerName,
    Object routeValues,
    Object htmlAttributes
)
而第二个代码称之为:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    Object routeValues,
    Object htmlAttributes
)
请注意,第一个调用中的字符串参数
controllerName
在第二个调用中变为
routeValues
。字符串值“Product”被传递给路由值:使用字符串属性
Length
,此处的长度为7,因此在路由中得到的“Length=7”

考虑到第一种方法,您似乎已经交换了
routeValues
htmlAttributes
参数

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    string controllerName,
    Object routeValues,
    Object htmlAttributes
)
public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    Object routeValues,
    Object htmlAttributes
)