MVC 3 Routing and Action Links not following expected contextual Route

寵の児 提交于 2019-12-06 13:31:44
Iron Ninja

I found a bit of a way around this so that the context wouldn't be lost. Here's what I did to get this to work.

TestRoute changes to this:

routes.MapRoute(
    "TestRoute",
    "{path}/{controller}/{action}/{id}",
    new { controller = "Space", action = "Index", id = UrlParameter.Optional },
    new { path = @"TEST" },
    new string[] { "The.Namespace" });

Setting the constraint on path and removing it from the route makes this routing work. Now I can hit the /TEST/Space/Index and all my links generated from ActionLink behave as intended. Also on a related issue, I ended up adding the namespace specification in the map route, as the development environment required that be in there to properly route things to the TEST path.

Some of the info I found was on this page.

If you do:

@Html.ActionLink("Text", "Index", "Space")

That is going to match the first route in your collection (TestRoute). This is the default behavior.

If you want to choose a specific route then use @Html.RouteLink instead.

If you want to target a specific route, you could use RouteLink extension, it allows you to specify which exact route should be used to generate the link.

@Html.RouteLink("with Test", "TestRoute")
@Html.RouteLink("with Test", "TestRoute", new {controller="Space", action="Foo"})
@Html.RouteLink("without Test", "Default", new {controller="Space", action="Foo"})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!