Skip to content

Instantly share code, notes, and snippets.

@thdotnet
Last active November 2, 2016 09:00
Show Gist options
  • Save thdotnet/dbb3afdd831582721b3096247ca5193b to your computer and use it in GitHub Desktop.
Save thdotnet/dbb3afdd831582721b3096247ca5193b to your computer and use it in GitHub Desktop.
Webapi Route Prefix
public class ApiVersion1RoutePrefixAttribute : RoutePrefixAttribute
{
private const string RouteBase = "api/{apiVersion:apiVersionConstraint(v1)}";
private const string PrefixRouteBase = RouteBase + "/";
public ApiVersion1RoutePrefixAttribute(string routePrefix)
: base(string.IsNullOrWhiteSpace(routePrefix) ? RouteBase : PrefixRouteBase + routePrefix)
{
}
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
var constraintsResolver = new DefaultInlineConstraintResolver();
constraintsResolver.ConstraintMap.Add
("apiVersionConstraint", typeof
(ApiVersionConstraint));
config.MapHttpAttributeRoutes(constraintsResolver);
config.Services.Replace(typeof (IHttpControllerSelector),
new NamespaceHttpControllerSelector(config));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment