Skip to content

Instantly share code, notes, and snippets.

@jeremiahredekop
Created October 27, 2011 20:02
Show Gist options
  • Save jeremiahredekop/1320697 to your computer and use it in GitHub Desktop.
Save jeremiahredekop/1320697 to your computer and use it in GitHub Desktop.
weakly typed MapServiceRoute method for webapi
using System.Linq;
using Microsoft.ApplicationServer.Http;
using Microsoft.ApplicationServer.Http.Activation;
namespace System.Web.Routing
{
public static class CustomRouteCollectionExtensions
{
public static void MapServiceRoute(this RouteCollection routes, Type serviceType, string routePrefix, HttpConfiguration configuration = null, object constraints = null, bool useMethodPrefixForHttpMethod = true)
{
if (routes == null)
throw new ArgumentNullException("routes");
if (configuration == null)
configuration = routes.GetDefaultHttpConfiguration();
var serviceHostFactory = new HttpServiceHostFactory {Configuration = configuration};
var webApiRoute = new WebApiRoute(routePrefix, serviceHostFactory, serviceType)
{Constraints = new RouteValueDictionary(constraints)};
routes.Add(webApiRoute);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment