mccalltd / AttributeRouting

Define your routes using attributes on actions in ASP.NET MVC and Web API.
http://mccalltd.github.io/AttributeRouting/
MIT License
416 stars 89 forks source link

Different route for specific parameter value #265

Open Daniel15 opened 11 years ago

Daniel15 commented 11 years ago

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 commented 11 years ago

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.