I've been writing a blog as a learning project for a while now and I've just rewritten my URL structure in order to improve the organisation of my controllers. This has gone fairly smoothly, but I have a little problem with a conflicting route.
I'm trying to setup my URL structure as follows:
/
/page/2
/category
/category/page/2
The categories are stored within the database. This works fine at the moment, but I just noticed that when I try to link back to the home page that it's hitting /page
instead.
Here's my current route table:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Admin",
"admin",
new { controller = "Admin", action = "Index" }
);
routes.MapRoute(
"ShowPagedPostsByCategory",
"{category}/page/{page}",
new { controller = "Posts", action = "Index", page = UrlParameter.Optional },
new { page = @"(\d+)?" }
);
routes.MapRoute(
"ShowPagedPosts",
"page/{page}",
new { controller = "Posts", action = "Index", page = UrlParameter.Optional },
new { page = @"(\d+)?" }
);
routes.MapRoute(
"ShowPostsByCategory",
"{category}",
new { controller = "Posts", action = "Index" }
);
routes.MapRoute(
"ShowTaggedPosts",
"posts/tagged/{tag}",
new { controller = "Posts", action = "ShowTaggedPosts", tag = UrlParameter.Optional }
);
routes.MapRoute(
"EditDeleteComment",
"posts/{action}/{id}",
new { controller = "Posts" },
new { action = @"EditComment|DeleteComment", id = @"\d+" }
);
routes.MapRoute(
"AddComment",
"{controller}/comment",
new { controller = "Posts", action = "Comment" }
);
routes.MapRoute(
"ShowPost",
"{control开发者_Go百科ler}/{PostID}/{*slug}",
new { controller = "Posts", action = "ShowPost", slug = UrlParameter.Optional },
new { PostID = @"\d+" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Posts", action = "Index", id = UrlParameter.Optional }
);
}
I can see what the problem is: the home URL of '/' is matching with the ShowPagedPosts route, but moving that below the default route seems to be the wrong thing to do. That makes me think my approach to this is a bit off. Can anyone point me in the right direction please?
Edit: Actually, with RouteDebugger I can see that it's actually matching the ShowPagedPosts and ShowPostsByCategory routes.
When using Html.ActionLink, the first matching route will be used.
If you want to use another specific route, use Html.RouteLink which takes the route's name as a parameter.
精彩评论