简体   繁体   English

Asp.net mvc 路由:ActionLink 返回一个带有查询字符串参数的 url

[英]Asp.net mvc routing : ActionLink return an url with parameters in querystring

I try to have an URL like this /Forum/Index/2我尝试使用这样的 URL /Forum/Index/2

for url I have a route {controller}/{action}/{page} in my global.asax对于 url,我在global.asax有一个路由{controller}/{action}/{page}

If I test the above URL with the Route Debugger it corresponds to the above route ( and some other but this is the first one in the list )如果我使用 Route Debugger 测试上面的 URL,它对应于上面的路由(还有其他一些,但这是列表中的第一个)

but if I create an url with the ActionLink ( like this : <%= Html.ActionLink(item.Title, "Index", new { controller = "Forum", page = page })%> ), this methode return me this URL /Forum/Index?page=2但是如果我用 ActionLink 创建一个 url (像这样: <%= Html.ActionLink(item.Title, "Index", new { controller = "Forum", page = page })%> ),这个方法返回给我这个URL /Forum/Index?page=2

Is there a way to have an URL with nothing in querystring with the ActionLink method?有没有办法使用 ActionLink 方法在查询字符串中创建一个没有任何内容的 URL?

This are all my routes and the match route is named DefaultWithPager :这是我所有的路由,匹配路由被命名为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
);

Gauthier高捷

It sounds like your route registration is just fine.听起来您的路线注册没问题。 You just need to use the Html.RouteLink helper and tell it the name of the route to use:您只需要使用 Html.RouteLink 助手并告诉它要使用的路由的名称:

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

The way Mvc routing works is that it evaluates each of the routes in order and will use the first one that matches. Mvc 路由的工作方式是按顺序评估每条路由,并使用第一个匹配的路由。 In your case the first one to match will be the:在您的情况下,第一个匹配的将是:

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

As that route doesn't have any mappings for page it will append it on to the url as get parameters.由于该路由没有任何页面映射,它会将其作为获取参数附加到 url 上。

If you want it to hit the one with the page mapping that should come before any other routes that will match the route data you're passing in.如果您希望它点击页面映射,该页面映射应该在任何其他与您传入的路由数据匹配的路由之前出现。

It should always go from most to least specific.它应该总是从最具体到最不具体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM