Example #1
0
 /* func */
 /// <summary>
 /// 获取目标 <see cref="Controller"/> 子继承类型中,所有具有 <see cref="WebAPIAttribute"/> + <see cref="ActionMethodSelectorAttribute"/> 特性的公开实例方法
 /// <para>如果不满足 <see cref="Controller"/> 子继承类型,返回空迭代器</para>
 /// <para>如果不具有满足条件的方法,返回空迭代器</para>
 /// </summary>
 /// <param name="type">目标类型</param>
 /// <returns>WebAPI 实例</returns>
 public static IEnumerable <WebAPIAttribute> GetWebAPI(Type type)
 {
     if (!typeof(Controller).IsAssignableFrom(type) ||
         type.IsAbstract)
     {
         yield break;
     }
     foreach (MethodInfo methodInfo in type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
     {
         WebAPIAttribute webAPIAttribute = methodInfo.GetCustomAttribute <WebAPIAttribute>();
         if (webAPIAttribute == null)
         {
             continue;
         }
         ActionMethodSelectorAttribute actionMethodSelector = methodInfo.GetCustomAttribute <ActionMethodSelectorAttribute>(true);
         if (actionMethodSelector != null)
         {
             webAPIAttribute.BindMethodInfo = methodInfo;
             yield return(webAPIAttribute);
         }
     }
 }
        /// <summary>
        /// Gets the accept verbs of an action
        /// </summary>
        /// <param name="methodInfo">The action's method information</param>
        /// <returns></returns>
        public static string GetAcceptVerbs(MethodInfo methodInfo)
        {
            if (!methodInfo.IsPublic)
            {
                return("[None]");
            }

            ActionMethodSelectorAttribute attribute = methodInfo.GetCustomAttributes(typeof(ActionMethodSelectorAttribute), true).Cast <ActionMethodSelectorAttribute>().FirstOrDefault();

            if (attribute == null)
            {
                return("All");
            }

            if (attribute is AcceptVerbsAttribute)
            {
                return(((AcceptVerbsAttribute)attribute).Verbs.StringJoin());
            }

            if (attribute is HttpDeleteAttribute)
            {
                return(HttpVerbs.Delete.ToString());
            }

            if (attribute is HttpGetAttribute)
            {
                return(HttpVerbs.Get.ToString());
            }

            if (attribute is HttpHeadAttribute)
            {
                return(HttpVerbs.Head.ToString());
            }

            if (attribute is HttpOptionsAttribute)
            {
                return(HttpVerbs.Options.ToString());
            }

            if (attribute is HttpPatchAttribute)
            {
                return(HttpVerbs.Patch.ToString());
            }

            if (attribute is HttpPostAttribute)
            {
                return(HttpVerbs.Post.ToString());
            }

            if (attribute is HttpPutAttribute)
            {
                return(HttpVerbs.Put.ToString());
            }

            if (attribute is NonActionAttribute)
            {
                return(string.Empty);
            }

            return(string.Empty);
        }