AttributeRouting
AttributeRouting copied to clipboard
Different route for specific parameter value
I have the following ASP.NET MVC routing rules:
routes.MapRoute(
name: "BlogCategory",
url: "category/{slug}",
defaults: new { controller = "Blog", action = "Category", page = 1 }
);
routes.MapRoute(
name: "BlogCategoryPage",
url: "category/{slug}/page-{page}",
defaults: new { controller = "Blog", action = "Category" }
);
When used with Url.Action
, this generates URLs like "/category/hello-world" when the page
parameter is 1, otherwise it generates URLs like "/category/hello-world/page-2". How do I replicate this in AttributeRouting? I've got the following attributes:
[GET("category/{slug}", ActionPrecedence = 1, RouteName = "BlogCategory")]
[GET("category/{slug}/page-{page:int}", ActionPrecedence = 2, RouteName = "BlogCategoryPage")]
But my Url.Action
calls are generating "/category/hello-world/page-1" for the first page.
Here's another one I pulled from my code:
context.MapRoute(
name: "BlogAdminPostsPublished",
url: "blog/admin/posts/published",
defaults: new { controller = "Blog", action = "Posts", published = true }
);
context.MapRoute(
name: "BlogAdminPostsUnpublished",
url: "blog/admin/posts/unpublished",
defaults: new { controller = "Blog", action = "Posts", published = false }
);
I can't work out how to route these at all.