加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 用于枚举的IRouteConstraint

发布时间:2020-12-15 19:52:22 所属栏目:asp.Net 来源:网络整理
导读:我想创建一个IRouteConstraint,它根据枚举的可能值过滤值. 我试图为自己谷歌,但这并没有导致任何结果. 有任何想法吗? 解决方法 见 this 基本上,你需要 private Type enumType; public EnumConstraint(Type enumType) { this.enumType = enumType; } public
我想创建一个IRouteConstraint,它根据枚举的可能值过滤值.
我试图为自己谷歌,但这并没有导致任何结果.

有任何想法吗?

解决方法

见 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());
  }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读