-1

I have a problem with Html.ActionLinkmethod.

If I use

@Html.ActionLink("Some text", "MyAction", "MyController", new { id = 1234 }, null)

I get a link with this href:

http://web.com/MyController/MyAction/1234

That is fine, but if I use more route values like

@Html.ActionLink("Some text", "MyAction", "MyController", new { id = 1234, param1 = 3, param2 = 10 }, null)

I get a link with this href:

http://web.com/MyController/MyAction/1234?param1=3&param2=10

But I need:

http://web.com/MyController/MyAction/1234/3/10

Do you know how can I get it?

Edition to give more info:

In MyController code file I have that:

[Route("MyController/MyAction/{id}")]
public ActionResult MyAction(string id) { /* some code */ }

[Route("MyController/MyAction/{id}/{param1}/{param2}")]
public ActionResult MyAction(string id, byte param1, byte param2) { /* some code */ }

And this is my RouteCofig.cs file:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapMvcAttributeRoutes();

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}
Jon
  • 891
  • 13
  • 32
  • I have this attribute `[Route("MyController/MyAction/{id}/{param1}/{param2}")]` in the action `public ActionResult MyAction(string id, byte param1, byte param2)`, I thought that there were the same thing. – Jon Oct 24 '16 at 11:11
  • can you show us your route config? – Mihai Alexandru-Ionut Oct 24 '16 at 11:24
  • Question edited with controller and route config. Am I missing something? – Jon Oct 24 '16 at 11:50
  • Please try use route from my answer. – Mihai Alexandru-Ionut Oct 24 '16 at 12:08
  • Yes, it is "`MyControllerController"`. If I use Alexandru's answer it works, but I would like to unsdertand what is the difference between adding a `MapRoute` in `RouteConfig` or using the `Route` attribute in the controller. – Jon Oct 24 '16 at 13:00

1 Answers1

1

Here is your solution,

routes.MapRoute(
        name: "MyRoute",                                           // Route name
        url: "{controller}/{action}/{id}/{param1}/{param2}",                          // URL with parameters
        defaults: new { controller = "MyController", action = "MyAction", id = "", param1="", param2="" }  // Parameter defaults
);
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128