Html.ActionLink添加params?和&而不是斜线

时间:2013-08-29 18:39:38

标签: asp.net-mvc-4 asp.net-mvc-routing razor-2 html.actionlink

这是我的简单动作链接:

<span>@Html.ActionLink(trip.TripDescription, "Index", "Home", new { trip = trip.Trpp, year = trip.TripYear })</span>

以下是该链接的路线:

routes.MapRoute(
                "Index",
                "{controller}/{action}/{trip}/{year}",
                new { controller = "Home", action = "Index", trip = "", year = "" }
            );

以下是正在呈现的内容:

http://localhost:31065/Home/Index?trip=Green&year=2013

我想让它改为:

http://localhost:31065/Home/Index/Green/2013

这两个网址都有效,但在美观上我更喜欢后者。

我需要更改什么才能让参数在斜杠后面而不是使用旧学校?和&amp;

TIA

1 个答案:

答案 0 :(得分:1)

看起来您的Index路线从未被击中,可能是由于之前定义的其他路线。始终保留最具体的路径定义,并保留最后定义的默认Controller/Action/Id

routes.MapRoute(
    "Index",
    "{controller}/{action}/{trip}/{year}",
    new { controller = "Home", action = "Index" }
);

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