MVC 3 Routing and Action Links not following expec

2019-05-23 16:38发布

I'm attempting to do a custom route so I can prefix a url in my application with a chosen string and the do some processing based on that. The problem I'm running into is, that the action links that are generated are not contextualized based on the url that it exists on.

Routes:

routes.MapRoute(
            "TestRoute",
            "TEST/{controller}/{action}/{id}",
            new { controller = "Space", action = "Index", id = UrlParameter.Optional });

routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Space", action = "Index", id = UrlParameter.Optional });

Navigating to TEST/Space/Index works, as well as Space/Index, but the odd issue I need fixed is that the links generated via ActionLink do not obey the context in which they are loaded, at least for the {controller}/{action}/{id} default route. Pages that are loaded under TEST/Space/Index list links properly, but when /Space/Index is loaded, they are all referencing the TEST/ route that the calling url does not. Is this the default behavior? Is there a way to get these links to generate in the proper context?

Edit:

The first place I saw this was in the Html.BeginForm without the TEST/

Html.BeginForm("ToTheMoon", "Space", FormMethod.Post)

which renders the link as TEST/Space/ToTheMoon

but it also shows up in links:

@Html.ActionLink("Take Me To The Space Port", "SpacePort", "Space")

which renders TEST/Space/SpacePort

3条回答
女痞
2楼-- · 2019-05-23 17:01

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.

查看更多
手持菜刀,她持情操
3楼-- · 2019-05-23 17:03

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.

查看更多
\"骚年 ilove
4楼-- · 2019-05-23 17:16

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"})
查看更多
登录 后发表回答