具有多个路由值的Html.ActionLink

时间:2016-10-24 10:55:46

标签: asp.net asp.net-mvc razor actionlink html.actionlink

我的Html.ActionLink方法存在问题。

如果我使用

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

我得到了一个关于这个href的链接:

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

没关系,但如果我使用更多的路线值,比如

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

我得到了一个关于这个href的链接:

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

但我需要:

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

你知道我怎么能得到它?

版本提供更多信息:

在MyController代码文件中,我有:

[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 */ }

这是我的RouteCofig.cs文件:

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

1 个答案:

答案 0 :(得分:1)

这是您的解决方案,

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