C# Ajax.ActionLink和;行动“;参数-奇怪的行为

C# Ajax.ActionLink和;行动“;参数-奇怪的行为,c#,ajax,asp.net-mvc-5,actionlink,C#,Ajax,Asp.net Mvc 5,Actionlink,我在一个项目中使用Ajax.ActionLinkhelper调用了几个Ajax,看起来像: @Ajax.ActionLink("Edit", "MyEditAction", "MyController", new {id = item.Id, action = item.Action}, new AjaxOptions {HttpMethod = "POST"}) 奇怪的是,action参数从URL参数中过滤出来,即使我将其重写为@action=item.action。但将此参数重命名为act

我在一个项目中使用
Ajax.ActionLink
helper调用了几个Ajax,看起来像:

@Ajax.ActionLink("Edit", "MyEditAction", "MyController", new {id = item.Id, action = item.Action}, new AjaxOptions {HttpMethod = "POST"})
奇怪的是,
action
参数从URL参数中过滤出来,即使我将其重写为
@action=item.action
。但将此参数重命名为
act
可以解决问题


有人知道为什么会这样吗?

我做了一些测试,action参数总是被ActionLink的action参数覆盖。 我认为框架中有一些东西覆盖了这个参数,使您的参数变得无用

我对
控制器
参数做了相同的测试,如下所示

 @Html.ActionLink("Register", "ActionName",routeValues: new {@action="fakeAction".ToString(CultureInfo.InvariantCulture), controller="fakeController".ToString(CultureInfo.InvariantCulture)})
这些参数覆盖当前控制器并断开在
fakeAction
controller中搜索的actionLink链接

public ActionResult Register(string action)
{
      return View();
}
如果我在控制器中只留下动作参数

public ActionResult Register(string action)
{
      return View();
}

输入中的操作参数总是
“Register”
,而不是
“fakeAction”
这个问题深入到.NET框架中。在
System.Web.Mvc
assembly中有一个类
routeValueShellers
,它有以下方法

public static RouteValueDictionary MergeRouteValues(string actionName, string controllerName, RouteValueDictionary implicitRouteValues, RouteValueDictionary routeValues, bool includeImplicitMvcValues)
{
    // Create a new dictionary containing implicit and auto-generated values
    RouteValueDictionary mergedRouteValues = new RouteValueDictionary();

    if (includeImplicitMvcValues)
    {
        // We only include MVC-specific values like 'controller' and 'action' if we are generating an action link.
        // If we are generating a route link [as to MapRoute("Foo", "any/url", new { controller = ... })], including
        // the current controller name will cause the route match to fail if the current controller is not the same
        // as the destination controller.

        object implicitValue;
        if (implicitRouteValues != null && implicitRouteValues.TryGetValue("action", out implicitValue))
        {
            mergedRouteValues["action"] = implicitValue;
        }

        if (implicitRouteValues != null && implicitRouteValues.TryGetValue("controller", out implicitValue))
        {
            mergedRouteValues["controller"] = implicitValue;
        }
    }

    // Merge values from the user's dictionary/object
    if (routeValues != null)
    {
        foreach (KeyValuePair<string, object> routeElement in GetRouteValues(routeValues))
        {
            mergedRouteValues[routeElement.Key] = routeElement.Value;
        }
    }

    // Merge explicit parameters when not null
    if (actionName != null)
    {
        mergedRouteValues["action"] = actionName;
    }

    if (controllerName != null)
    {
        mergedRouteValues["controller"] = controllerName;
    }

    return mergedRouteValues;
}
公共静态RouteValueDictionary合并routeValues(string actionName、string controllerName、RouteValueDictionary隐式路由值、RouteValueDictionary routeValues、bool includeImplicitMvcValues)
{
//创建包含隐式值和自动生成值的新词典
RouteValueDictionary MergedLooteValues=新的RouteValueDictionary();
if(包括IMPLICITMVC值)
{
//如果我们正在生成一个动作链接,那么我们只包括MVC特定的值,如“控制器”和“动作”。
//如果我们正在生成一个路由链接[关于MapRoute(“Foo”,“any/url”,new{controller=…})],包括
//如果当前控制器不同,则当前控制器名称将导致路由匹配失败
//作为目标控制器。
客体隐含价值;
if(implicitRouteValues!=null&&implicitRouteValues.TryGetValue(“操作”,out implicitValue))
{
MergedLoteValues[“action”]=隐式值;
}
if(implicitRouteValues!=null&&implicitRouteValues.TryGetValue(“控制器”,out implicitValue))
{
MergedLoteValues[“控制器”]=隐式值;
}
}
//合并用户字典/对象中的值
if(routeValue!=null)
{
foreach(GetRouteValue(RouteValue)中的KeyValuePair routeElement)
{
MergeDuroteValues[routeElement.Key]=routeElement.Value;
}
}
//不为null时合并显式参数
if(actionName!=null)
{
MergeDuroteValues[“action”]=actionName;
}
if(controllerName!=null)
{
mergedLoteValues[“controller”]=controllerName;
}
返回合并值;
}
它由
UrlHelper.GenerageUrl
方法调用

到目前为止,我们看到隐式或显式操作/控制器名称将替换路由值中的
action
controller
参数,即使它应该是查询字符串或其他url参数


我不知道M$为什么这样做,但我想这更像是一个bug而不是功能。请注意,我已经找到了问题的根源,请参阅上面的答案。感谢您解决了这个问题,知道这是件好事!