我想创建一个IRouteConstraint,以针对枚举的可能值过滤值。
我尝试自己为Google进行搜索,但这没有任何结果。

有任何想法吗?

最佳答案

this

本质上,您需要

  private Type enumType;

  public EnumConstraint(Type enumType)
  {
    this.enumType = enumType;
  }

  public bool Match(HttpContextBase httpContext,
    Route route,
    string parameterName,
    RouteValueDictionary values,
    RouteDirection routeDirection)
  {
    // You can also try Enum.IsDefined, but docs say nothing as to
    // is it case sensitive or not.
    return Enum.GetNames(enumType).Any(s => s.ToLowerInvariant() == values[parameterName].ToString());
  }

08-26 00:03