ActionLink - 指定控制器名称时的奇怪行为

时间:2014-08-01 15:26:48

标签: c# asp.net-mvc-4 extension-methods actionlink

我有以下扩展方法:

public static MvcHtmlString GenerateBodyCellContentFor(
    this HtmlHelper helper,
    Goal goal,
    GoalProperty property)
    {
        if (property == GoalProperty.Name)
        {
            return LinkExtensions.ActionLink(
                helper,
                goal.Name,
                "Show",
                new
                {
                    goalId = goal.GoalId
                });
        }

        // rest of the code irrelevant
    }

这可以按预期工作并生成以下链接:

http://localhost:54913/Goal/Show?goalId=19013

由于某些情况,我现在需要明确指定控制器名称。所以我将方法改为:

public static MvcHtmlString GenerateBodyCellContentFor(
    this HtmlHelper helper,
    Goal goal,
    GoalProperty property)
    {
        if (property == GoalProperty.Name)
        {
            return LinkExtensions.ActionLink(
                helper,
                goal.Name,
                "Show",
                "Goal",                           // the only change in code
                new
                {
                    goalId = goal.GoalId
                });
        }

        // rest of the code irrelevant
    }

结果令我惊讶的是:

http://localhost:54913/Goal/Show?Length=4

我尝试删除并添加控制器参数三次,因为我无法相信这实际发生了。这是唯一的变化,它会导致此行为。

length参数等于控制器名称的长度(只是用不同的字符串检查)。

你知道可能会发生什么吗?

1 个答案:

答案 0 :(得分:0)

正如this question的答案所解释的那样,问题是由于没有任何重载符合我需要它的方式。解决这个问题最简单的方法是:

return LinkExtensions.ActionLink(
    helper,
    goal.Name,
    "Show",
    "Goal",
    new
    {
        goalId = goal.GoalId
    },
    null);
相关问题