Last active
December 16, 2015 12:28
-
-
Save sebnilsson/5434495 to your computer and use it in GitHub Desktop.
RouteConstraint to ensure a parameter is a Guid
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
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