Created
August 6, 2012 13:51
-
-
Save benfoster/3274578 to your computer and use it in GitHub Desktop.
Enforcing lower case routes in ASP.NET Web Api
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 RouteConfig | |
{ | |
public static void RegisterRoutes(RouteCollection routes) | |
{ | |
routes.MapHttpRoute( | |
name: "DefaultApi", | |
routeTemplate: "api/{controller}/{id}", | |
defaults: new { id = RouteParameter.Optional }, | |
constraints: new { url = new LowercaseRouteConstraint() } | |
); | |
} | |
} | |
public class LowercaseRouteConstraint : IRouteConstraint | |
{ | |
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) | |
{ | |
var path = httpContext.Request.Url.AbsolutePath; | |
return path.Equals(path.ToLowerInvariant(), StringComparison.InvariantCulture); | |
} | |
} |
Is there any way to enforce lowercase on Links generated? To be accommodating in what is accepted and precise in what is outputed.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that this doesn't enforce lower case querystring parameters, although it probably should.