Skip to content

Instantly share code, notes, and snippets.

@benfoster
Created August 6, 2012 13:51
Show Gist options
  • Save benfoster/3274578 to your computer and use it in GitHub Desktop.
Save benfoster/3274578 to your computer and use it in GitHub Desktop.
Enforcing lower case routes in ASP.NET Web Api
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);
}
}
@benfoster
Copy link
Author

Note that this doesn't enforce lower case querystring parameters, although it probably should.

@ngbrown
Copy link

ngbrown commented Mar 4, 2015

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