Asp.net mvc routing:ActionLink用querystring返回一个包含参数的url

Helo,我尝试拥有一个类似于/ Forum / Index / 2的URL

为url我有一个路线{控制器} / {动作} / {页}在我的global.asax

如果我用Route Debugger测试上面的url,它对应于上面的路由(和其他一些,但这是列表中的第一个),但是如果我使用ActionLink创建一个url(如下所示:<%= Html.ActionLink( item.Title,“Index”,新的{controller =“论坛”,page = page})%>),这种方法返回给我这个URL /论坛/索引?page = 2

使用ActionLink方法有没有办法在查询字符串中包含任何内容?

这是我的所有路线,匹配路线名为DefaultWithPage:

routes.IgnoreRoute( “{}资源个.axd / {*} PATHINFO”); routes.IgnoreRoute( “google884930bf56853ce4.html”);

        routes.MapRoute(
            "sitemapXML",
            "Sitemap.xml",                                                                                      // URL with parameters
            new { controller = "Sitemap", action = "Sitemap" }                                                  // Parameter defaults
        );

        routes.MapRoute(
            "ImageStat",                                                                                        // Route name
            "{controller}/{action}/{MailID}/{UserID}.gif",                                                      // URL with parameters
            new { controller = "", action = "", MailID = "", UserID = "" },                                     // Parameter defaults
            new { 
                MailID = @"^({{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}}{0,1})$", 
                UserID = @"^({{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}}{0,1})$" 
            }
        );

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

        routes.MapRoute(
            "DefaultWithPager",                                                                                 // Route name
            "{controller}/{action}/{page}",                                                                     // URL with parameters
            new { controller = "Home", action = "", page = "" },                                                // Parameter defaults
            new { page = @"^d+$" }  
        );

        routes.MapRoute(
            "DefaultWithID",                                                                                    // Route name
            "{controller}/{action}/{ID}",                                                                       // URL with parameters
            new { controller = "Home", action = "", ID = "" },                                                  // Parameter defaults
            new { ID = @"^({{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}}{0,1})$" }
        );

        routes.MapRoute(
            "DetailWithID",                                                                                     // Route name
            "{controller}/{action}/{ID}/{Title}",                                                               // URL with parameters
            new { controller = "Home", action = "", ID = "", Title = "" },                                      // Parameter defaults
            new { ID = @"^({{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}}{0,1})$" }
        );

        //Handler route
        routes.MapRoute(
            "ImageResizer",                                                                                     // Route name
            "{controller}/{action}/{Title}/{ID}.jpg",                                                           // URL with parameters
            new { controller = "", action = "", Title = "", ID = "" }                                           // Parameter defaults
        );


        //Section Pages Routes
        routes.MapRoute(
            "SectionIndex",                                                                                     // Route name
            "{sectionName}/{controller}/{action}",                                                              // URL with parameters
            new { controller = "", action = "", sectionName = "" }                                              // Parameter defaults                
        );

        routes.MapRoute(
            "SectionDetails",                                                                                   // Route name
            "{sectionName}/{controller}/{action}/{ID}",                                                         // URL with parameters
            new { controller = "", action = "", sectionName = "", ID = "" }                                     // Parameter default
        );

        routes.MapRoute(
            "SectionDetailsWithDate",                                                                           // Route name
            "{sectionName}/{controller}/{action}/{month}/{year}",                                               // URL with parameters
            new { controller = "Home", action = "", page = "", month = "", year = "" },                         // Parameter defaults
            new { month = @"^d+$", year = @"^d+$" }
        );

        routes.MapRoute(
            "SectionDetailsWithTitle",                                                                          // Route name
            "{sectionName}/{controller}/{action}/{ID}/{Title}",                                                 // URL with parameters
            new { controller = "", action = "", sectionName = "", ID = "", Title = "" }                         // Parameter defaults
        );

        routes.MapRoute(
            "SectionDetailsPagingWithTitle",                                                                    // Route name
            "{sectionName}/{controller}/{action}/{page}/{ID}/{Title}",                                          // URL with parameters
            new { controller = "", action = "", sectionName = "", page = "", ID = "", Title = "" }              // Parameter defaults
        );

        routes.MapRoute(
            "SectionDetailsPaging",                                                                             // Route name
            "{sectionName}/{controller}/{action}/{page}/{ID}",                                                  // URL with parameters
            new { controller = "", action = "", sectionName = "", page = "", ID = "" }                          // Parameter defaults
        );

戈捷


这听起来像你的路线注册就好了。 你只需要使用Html.RouteLink助手并告诉它要使用的路由的名称:

<%= Html.RouteLink(item.Title, "DefaultWithPager", new { controller = "Forum", page = page })%>

Mvc路由工作的方式是按顺序评估每条路由,并使用第一条匹配的路由。 在你的情况下,第一个匹配将是:

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

由于该路由没有任何页面映射,因此它会将其作为get参数附加到url上。

如果您希望它打到页面映射的页面映射,该映射应位于与您要传入的路由数据相匹配的任何其他路由之前。

它应该始终从最具体到最不具体。

链接地址: http://www.djcxy.com/p/49981.html

上一篇: Asp.net mvc routing : ActionLink return an url with parameters in querystring

下一篇: How do I stop URL.Action from including parameters?