Skip to content

Instantly share code, notes, and snippets.

@sebnilsson
Last active December 16, 2015 12:28
Show Gist options
  • Save sebnilsson/5434495 to your computer and use it in GitHub Desktop.
Save sebnilsson/5434495 to your computer and use it in GitHub Desktop.
RouteConstraint to ensure a parameter is a Guid
private class GuidRouteConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
object value;
if (!values.TryGetValue(parameterName, out value))
{
return false;
}
object defaultValue;
if (route.Defaults.TryGetValue(parameterName, out defaultValue) && (value == defaultValue))
{
return true;
}
Guid guid;
return Guid.TryParse(value.ToString(), out guid);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment