Custom names for routes and actions

May 20, 2011 at 7:49 AM

Hi Max,  

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


namespace Site.Blog {

  class BlogController : Controller
      public ActionResult SomeAction(){}

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 :(


May 20, 2011 at 3:41 PM
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 ?
May 20, 2011 at 3:45 PM

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...

May 20, 2011 at 3:56 PM
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"
May 23, 2011 at 1:58 PM

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.

May 24, 2011 at 3:48 AM
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.