Map Service Routes by convention

Topics: Web Api
Nov 25, 2011 at 7:50 PM
Edited Nov 25, 2011 at 8:07 PM

I like the principle "favorite convention over configuration", which is widely used in MVC Framework.

So i wrote a little (barely tested) extension method, that allows an automated ServiceRoute mapping of this kind:

 

            // Set configuration & routes
            RouteTable.Routes.SetDefaultHttpConfiguration(WebApiConfiguration);
            RouteTable.Routes.AutoMapServiceRoutes();
            //instead of...
            //RouteTable.Routes.MapServiceRoute<HomeController>("home");
            //RouteTable.Routes.MapServiceRoute<TasksApi>(TasksApi.ResourcePath);
            //RouteTable.Routes.MapServiceRoute<ProcessesApi>(ProcessesApi.ResourcePath);
            //etc..

 

The (reflection) magic happens here in the RouteCollectionExtensions.cs:

 

public static void AutoMapServiceRoutes(this RouteCollection routes, string servicePostfix="Api", HttpConfiguration configuration = null, object constraints = null, bool useMethodPrefixForHttpMethod = true)
         {
             var apis = from t in Assembly.GetExecutingAssembly().GetTypes()
                        where t.IsClass && !t.IsAbstract && t.Name.EndsWith(servicePostfix)
                        select t;
             foreach(var api in apis)
             {
                 var routePrefix = api.Name.EnsureEndsNotWith(servicePostfix).ToLower();
                 routes.MapServiceRoute(api, routePrefix, configuration, constraints, useMethodPrefixForHttpMethod);
             }
         }

         // non-generic version of MapServiceRoute<T>
         public static void MapServiceRoute(this RouteCollection routes, Type service, string routePrefix, HttpConfiguration configuration = null, object constraints = null, bool useMethodPrefixForHttpMethod = true)
         {
             if (configuration == null)
             {
                 configuration = defaultConfiguration;
             }


             if (routes == null)
             {
                 throw new ArgumentNullException("routes");
             }

             var route = new WebApiRoute(routePrefix, new HttpServiceHostFactory() { Configuration = configuration }, service);
             route.Constraints = new RouteValueDictionary(constraints);
             routes.Add(route);
         }

 

How do you like it?

Dec 13, 2011 at 7:00 PM

This is cool!  I was just about to ask if this is possible.  I'll give it a try.

Dec 13, 2011 at 7:02 PM

Can you post the entire class with the using references?

Dec 13, 2011 at 9:39 PM

I posted a sample MVC3 solution that includes a convention mechanism for service routes back in May.  You might want to check it out: http://wcf.codeplex.com/discussions/256581.

Dec 15, 2011 at 10:03 PM
davidpeden3 wrote:

I posted a sample MVC3 solution that includes a convention mechanism for service routes back in May.  You might want to check it out: http://wcf.codeplex.com/discussions/256581.

I just looked at your post and code.  It looks perfect!  I'll give it a shot.  Also, thanks for posting an example with your fork, it makes it a lot easier for folks with an IQ of 4 like myself to get up and running quicker.  :^)