AttributeRouting icon indicating copy to clipboard operation
AttributeRouting copied to clipboard

Different route for specific parameter value

Open Daniel15 opened this issue 11 years ago • 1 comments

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.

Daniel15 avatar Sep 14 '13 09:09 Daniel15

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.

Daniel15 avatar Sep 14 '13 09:09 Daniel15