MVC4路由怪异

时间:2012-05-22 17:27:28

标签: asp.net-mvc asp.net-mvc-4

我在我的一个观点中有这个ActionLink

Html.ActionLink(pop.PopName, "ShowAllEncounters", "Encounter", new {popId = pop.populationID })

我在想,这会带我走正确路线...... 但我转而走这条路......

http://localhost:19283/Population/ShowAllEncounters?Length=9

路线应为

http://localhost:19283/Encounter/ShowAllEncounters?Length=9

我查看了我的global.asax文件,一切看起来都很正常......除了设置默认的开始页面之外,我没有通过任何方式更改默认路由...

routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Population", action = "PopulationInfo", id = UrlParameter.Optional } // Parameter defaults
        );

这就是我的预期......这与我拥有的MVC3路线类似。我用我的路线做一些时髦的事吗?我在这里缺少什么?

2 个答案:

答案 0 :(得分:1)

看起来你正在调用错误的方法重载:

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

http://msdn.microsoft.com/en-us/library/dd492124.aspx

相反,请尝试调用此重载:

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

http://msdn.microsoft.com/en-us/library/dd504972.aspx


要调用正确的过载,请使用类似于以下内容的内容:

Html.ActionLink(pop.PopName,
                "ShowAllEncounters",
                "Encounter",
                null,
                new {popId = pop.populationID })

答案 1 :(得分:0)

您的代码

Html.ActionLink(pop.PopName, "ShowAllEncounters", "Encounter", new {popId = pop.populationID })

应该是

Html.ActionLink(pop.PopName, "ShowAllEncounters", new {controller="Encounter"}, new {popId = pop.populationID })