MVC 3 Routing and Actions Links do not match the expected contextual route

I'm trying to execute my own route, so I can specify the URL in my application with the selected line, and based on this, do some processing. The problem I am facing is that the created action links are not contextualized based on the URL at which it exists.

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 });

Working with TEST / Space / Index works, as well as Space / Index, but the odd problem I need to fix is ​​that the links created through ActionLink do not obey the context in which they are loaded, at least for {controller } / {action} / {id} by default. Pages that load according to the TEST / Space / Index lists are correct, but when / Space / Index loads, they all link to TEST / route, which the calling URL does not have. Is this the default behavior? Is there a way to get these links to generate in the appropriate context?

Edit:

The first thing I saw was in Html.BeginForm without TEST /

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

which displays the link as TEST / Space / ToTheMoon

but it also appears in the links:

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

which displays TEST / Space / SpacePort

+2
3

, . , .

TestRoute :

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

. /TEST/Space/Index, , ActionLink, , . , , TEST.

, , .

+4

:

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

(TestRoute). .

, @Html.RouteLink.

+3

If you want to target a specific route, you can use the extension RouteLink, it allows you to specify which route should be used to create a 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"})
0
source

All Articles