Custom names for routes and actions

Imported from the CodePlex archive for reference purposes. Support for MvcCodeRouting has ended.

Commented on

Hi Max,  

It would be great if the library could support attributes for custom-naming the routes. Something like:

 

namespace Site.Blog {

  [RouteName("my-blog")]
  class BlogController : Controller
  {
      [RouteName("do-something")]
      public ActionResult SomeAction(){}
  }
}
for: site.com/my-blog/do-something

In this case the attribute on the controller would override the namespace. The other option would be to have the attribute on the Index() action to override.

I'd like to use the library but my current site requirements for these naming conventions are a show stopper :(

-zul

Commented on link
For the action segment of the URL you can use the ActionName attribute.

In this case, do you want all routes to start with my-blog, or just
routes for this controller ?
Commented on link

I think you can still achieve what you need by manually adding routes before or after your call to routes.MapCoudeRoutes.  Here is a basic example:

//makes BlogController.SomeAction available at the url "my-blog/do-something".
routes.MapRoute("blog-do-something", "my-blog/do-something", new { controller = "Blog", action="SomeAction"});

//MvcCodeRouting generates routes here
routes.MapCodeRoutes(typeof(HomeController).Namespace, new CodeRoutingSettings { DefaultAction = "Index" });

It WOULD be nice to be able to tell MvcCodeRouting to ignore certain controllers, though, so you wouldn't have two urls pointing to the same resource...

Commented on link
Hey tyler,
What I've been thinking for a while now is having a base route setting
(need to think about the name since we are already using BaseRoute for
the namespace part of the route).

So, you could do something like:

routes.MapCodeRoutes("Site", new CodeRoutingSettings { BaseRoute = "my-blog" });

That way all routes would start with "my-blog"
Commented on link

Ah yes, totally forgot about the ActionName attribute. Thanks.

For my use case it'll be all routes will start with my-blog. Your solution for a base route setting makes sense to me.

What do you think of this as an additional way to declare base routes?

[RouteName("my-blog", NameSpaceBaseRoute: true)]
class BlogController : Controller

I know it doesn't make sense in a multiple controller per namespace scenario but the flexibility seems useful - at least for my usage!
I generally prefer attribute routing rather than since most of my routes have hyphens and knowing the routes/constraints on top of the action is useful.

Commented on link
I think this setting (base route or whatever name we use) should be in
control of the person that configures routing for the app, and not the
person who writes the controller. Usually they are the same person,
but I want to support plugin scenarios, e.g. a blog service written by
a 3rd party.