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

发布于 2024-08-17 18:51:15 字数 6289 浏览 4 评论 0原文

我尝试使用这样的 URL /Forum/Index/2

作为 url 我的 中有一条路线 {controller}/{action}/{page} 如果我使用 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
);

Gauthier

I try to have an URL like this /Forum/Index/2

for url I have a route {controller}/{action}/{page} in my global.asax

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 )

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

Is there a way to have an URL with nothing in querystring with the ActionLink method?

This are all my routes and the match route is named 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぺ禁宫浮华殁 2024-08-24 18:51:15

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

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

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(item.Title, "DefaultWithPager", new { controller = "Forum", page = page })%>
秋风の叶未落 2024-08-24 18:51:15

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

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

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

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

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

The way Mvc routing works is that it evaluates each of the routes in order and will use the first one that matches. 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文