Last active
November 2, 2016 09:00
-
-
Save thdotnet/dbb3afdd831582721b3096247ca5193b to your computer and use it in GitHub Desktop.
Webapi Route Prefix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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