GuidConstraint.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Web;
  7. using System.Web.Routing;
  8. namespace GreenTree.Nachtragsmanagement.Web.Framework.Mvc.Routes
  9. {
  10. public class GuidConstraint : IRouteConstraint
  11. {
  12. private readonly bool _allowEmpty;
  13. public GuidConstraint(bool allowEmpty)
  14. {
  15. this._allowEmpty = allowEmpty;
  16. }
  17. public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
  18. {
  19. if (values.ContainsKey(parameterName))
  20. {
  21. string stringValue = values[parameterName] != null ? values[parameterName].ToString() : null;
  22. if (!string.IsNullOrEmpty(stringValue))
  23. {
  24. Guid guidValue;
  25. return Guid.TryParse(stringValue, out guidValue) &&
  26. (_allowEmpty || guidValue != Guid.Empty);
  27. }
  28. }
  29. return false;
  30. }
  31. }
  32. }