1

我尝试拥有这样的网址/Forum/Index/2

对于 url ,我的路由{controller}/{action}/{page}global.asax

如果我使用 Route Debugger 测试上面的 URL,它对应于上面的路由(还有其他一些,但这是列表中的第一个)

但是如果我使用 ActionLink 创建一个 url(像这样 <%= Html.ActionLink(item.Title, "Index", new { controller = "Forum", page = page })%>:),这个方法返回我这个 URL/Forum/Index?page=2

有没有办法使用 ActionLink 方法在查询字符串中创建一个没有任何内容的 URL?

这是我的所有路线,匹配路线被命名为DefaultWithPager

routes.IgnoreRoute("{resource}.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
);

高捷

4

2 回答 2

4

听起来您的路线注册很好。您只需要使用 Html.RouteLink 帮助器并告诉它要使用的路线的名称:

<%= Html.RouteLink(item.Title, "DefaultWithPager", new { controller = "Forum", page = page })%>
于 2010-01-18T02:46:45.733 回答
0

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

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

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

如果您希望它与页面映射相匹配,该页面映射应该在与您传入的路由数据匹配的任何其他路由之前出现。

它应该总是从最具体到最不具体。

于 2016-05-31T14:01:06.040 回答