Пример #1
0
        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <typeparamm name="T"></typeparamm>
        /// <paramm name="handlerType">Type of the method.</paramm>
        /// <returns></returns>
        public static T GetAttribute <T>(MethodInfo method) where T : Attribute
        {
            var attributes = ActionBehaviorStorager.GetAttributes(method);

            for (var i = 0; i < attributes.Count(); i++)
            {
                var attribute = attributes.ElementAt(i) as T;
                if (attribute != null)
                {
                    return(attribute);
                }
            }

            return(default(T));
        }
Пример #2
0
        /// <summary>
        /// Selects the action.
        /// </summary>
        /// <param name="controllerContext">The controller context.</param>
        /// <returns></returns>
        public override HttpActionDescriptor SelectAction(HttpControllerContext controllerContext)
        {
            ActionResultMetadata?item = null;

            if (controllerContext.Request.Properties.ContainsKey("CurrentApiRoute"))
            {
                item = controllerContext.Request.Properties["CurrentApiRoute"] as ActionResultMetadata?;
            }
            else
            {
                item = customHttpControllerSelector.FindControllerAndActionName(controllerContext.Request);
            }

            if (!item.HasValue || item.Value.ControllerName.IsNullOrEmpty() || item.Value.ActionMethod == null)
            {
                return(base.SelectAction(controllerContext));
            }

            var attributes = ActionBehaviorStorager.GetAttributes(item.Value.ActionMethod);

            /*当为空的时候,则直接返回*/
            if (attributes == null || !attributes.Any())
            {
                return(new ReflectedHttpActionDescriptor(controllerContext.ControllerDescriptor, item.Value.ActionMethod));
            }

            /*匿名方法直接返回*/
            if (attributes.GetAttribute <AllowAnonymousAttribute>() != null)
            {
                return(new ReflectedHttpActionDescriptor(controllerContext.ControllerDescriptor, item.Value.ActionMethod));
            }

            /*遍历是否带上各种attribute*/
            switch ((controllerContext.Request.Method.Method ?? "get").ToLower())
            {
            case "get":
                {
                    if (attributes.GetAttribute <HttpGetAttribute>() != null)
                    {
                        return(new ReflectedHttpActionDescriptor(controllerContext.ControllerDescriptor, item.Value.ActionMethod));
                    }
                }
                break;

            case "head":
            {
                if (attributes.GetAttribute <HttpHeadAttribute>() != null)
                {
                    return(new ReflectedHttpActionDescriptor(controllerContext.ControllerDescriptor, item.Value.ActionMethod));
                }
            }
            break;

            case "options":
            {
                if (attributes.GetAttribute <HttpOptionsAttribute>() != null)
                {
                    return(new ReflectedHttpActionDescriptor(controllerContext.ControllerDescriptor, item.Value.ActionMethod));
                }
            }
            break;

            case "post":
            {
                if (attributes.GetAttribute <HttpPostAttribute>() != null)
                {
                    return(new ReflectedHttpActionDescriptor(controllerContext.ControllerDescriptor, item.Value.ActionMethod));
                }
            }
            break;

            case "put":
            {
                if (attributes.GetAttribute <HttpPutAttribute>() != null)
                {
                    return(new ReflectedHttpActionDescriptor(controllerContext.ControllerDescriptor, item.Value.ActionMethod));
                }
            }
            break;

            case "trace":
            {
                if (attributes.GetAttribute <HttpPatchAttribute>() != null)
                {
                    return(new ReflectedHttpActionDescriptor(controllerContext.ControllerDescriptor, item.Value.ActionMethod));
                }
            }
            break;

            case "delete":
            {
                if (attributes.GetAttribute <HttpDeleteAttribute>() != null)
                {
                    return(new ReflectedHttpActionDescriptor(controllerContext.ControllerDescriptor, item.Value.ActionMethod));
                }
            }
            break;
            }

            throw new HttpResponseException(controllerContext.Request.CreateErrorResponse(HttpStatusCode.MethodNotAllowed, string.Format("The requested resource does not support http method {0}", controllerContext.Request.Method.Method)));
        }