Html.actionlink没有生成正确的url

时间:2011-11-23 05:58:28

标签: asp.net-mvc-3

我在global.asax中的路线就像这样 -

 routes.MapRoute(
            "History", // Route name
            "{controller}/{PageID}/{LanguageID}/{PreviousRevisionID}/{NewRevisionID}", // URL with parameters
            new { controller = "UserArea", action = "Index", PageID = 0, LanguageID = 0, PreviousRevisionID = 0, NewRevisionID = 0} // Parameter defaults
        );

我正在使用Html.actionLink制作这样的网址。

@Html.ActionLink(item.CreatedTS.ToString(), "Index", new { PageID = item.PageID ,LanguageID = item.LanguageID , PreviousRevisionID = item.PreviousRevisionID , NewRevisionID = item.NewRevisionID , controller = "History" });

但是网址会生成如此

http://localhost:2582/History?PageID=2&LanguageID=3&PreviousRevisionID=98&NewRevisionID=103

但我想要网址像这样

http://localhost:2582/History/2/3/98/103

问题在哪里

2 个答案:

答案 0 :(得分:4)

使用RouteLink代替ActionLink:

@Html.RouteLink(item.CreatedTS.ToString(), "History", new { PageID = item.PageID ,LanguageID = item.LanguageID , PreviousRevisionID = item.PreviousRevisionID , NewRevisionID = item.NewRevisionID , controller = "History" });

对于差异,请参阅此SO问题。

答案 1 :(得分:0)

您似乎可能以错误的顺序定义了您的路线。您希望在默认路由之前定义此路由。所以你的路线可能看起来像这样。

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

        routes.MapRoute(
            "History", // Route name
            "{controller}/{PageID}/{LanguageID}/{PreviousRevisionID}/{NewRevisionID}", // URL with parameters
            new { controller = "UserArea", action = "Index", PageID = 0, LanguageID = 0, PreviousRevisionID = 0, NewRevisionID = 0 } // Parameter defaults
        );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Default", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    }

Phil Haack的路由调试器实用程序对调试路由非常有帮助。

http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx

PM> Install-Package routedebugger