示例#1
0
        /// <summary>
        /// Returns a particular portion of the RouteData
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="whichPart"></param>
        /// <returns></returns>
        public static String GetRouteData(this System.Web.Mvc.HtmlHelper htmlHelper, RouteParts whichPart)
        {
            Object obj;
            String returnValue = String.Empty;
            String key         = "action";

            if (whichPart == RouteParts.Area)
            {
                key = "area";
            }
            else if (whichPart == RouteParts.Controller)
            {
                key = "controller";
            }
            obj = htmlHelper.ViewContext.RouteData.Values[key];
            if (obj != null)
            {
                returnValue = obj.ToString( );
            }
            return(returnValue);
        }
        public ConstructorParameterMapping[] GetConstructorParameterMappings(bool ignoreEnumDefinitions = false)
        {
            var tempConstructorParameters = Method.Parameters
                                            .Select(p => new
            {
                Parameter = p,
                RoutePart = Controller.RouteParts.SingleOrDefault(brp => brp.ParameterName == p.Name)
                            ?? RouteParts.SingleOrDefault(rp => rp.ParameterName == p.Name)
                            ?? QueryStringParameters.SingleOrDefault(qsp => qsp.Name == p.Name)
            })
                                            .Where(cp => cp.RoutePart != null)
                                            .ToList();

            var constructorParameters = new List <WebApiRoutePart>();

            foreach (var tcp in tempConstructorParameters)
            {
                if (tcp.RoutePart.Parameter == null)
                {
                    tcp.RoutePart.Parameter = tcp.Parameter;
                }

                constructorParameters.Add(tcp.RoutePart);
            }

            var constructorParameterMappings = constructorParameters
                                               .Select(routePart => new ConstructorParameterMapping
            {
                IsOptional          = routePart.IsOptional && TypeService.IsParameterOptional(routePart.Parameter),
                TypeMapping         = routePart.GetTypeMapping(),
                Name                = routePart.Parameter.Name,
                StringWithOptionals = routePart.GetParameterString(interfaceName: true, ignoreEnumDefinitions: ignoreEnumDefinitions),
                String              = routePart.GetParameterString(withOptionals: false, interfaceName: true, ignoreEnumDefinitions: ignoreEnumDefinitions)
            })
                                               .OrderBy(p => p.IsOptional)
                                               .ToArray();

            return(constructorParameterMappings);
        }
        private void BuildQueryStringAndBodyRouteParts()
        {
            var actionParameters = Method.Parameters
                                   .Where(p => Controller.RouteParts.All(brp => brp.ParameterName != p.Name) &&
                                          RouteParts.All(rp => rp.ParameterName != p.Name))
                                   .ToList();

            var isBodyAllowed = Verbs.Contains(WebApiHttpVerb.Post) ||
                                Verbs.Contains(WebApiHttpVerb.Put);

            var fromBodyAttributeName = "FromBodyAttribute";
            var fromUriAttributeName  = "FromUriAttribute";

            var isThereAnythingFromBody = actionParameters
                                          .Any(ap => Helpers.HasCustomAttribute(ap, fromBodyAttributeName));

            foreach (var actionParameter in actionParameters)
            {
                var isFromBody  = Helpers.HasCustomAttribute(actionParameter, fromBodyAttributeName);
                var isFromUri   = Helpers.HasCustomAttribute(actionParameter, fromUriAttributeName);
                var isPrimitive = actionParameter.ParameterType.IsPrimitive;

                if (!isPrimitive)
                {
                    var strippedType = TypeService.StripGenerics(actionParameter.ParameterType, actionParameter.Name, out bool isNullable, out int collectionLevel);
                    isPrimitive = isNullable && TypeService.GetPrimitiveTypeScriptType(strippedType.FullName) != null;
                }

                if (isBodyAllowed &&
                    ((isThereAnythingFromBody && isFromBody) ||
                     (!isThereAnythingFromBody && !isFromUri && !isPrimitive)))
                {
                    BodyParameters.Add(new WebApiRoutePart
                    {
                        Name             = actionParameter.Name,
                        ParameterName    = actionParameter.Name,
                        Parameter        = actionParameter,
                        CustomAttributes = new List <string> {
                            fromBodyAttributeName
                        },
                        IsOptional = false
                    });
                }
                else
                {
                    var queryStringRoutePart = new WebApiRoutePart
                    {
                        Name          = actionParameter.Name,
                        ParameterName = actionParameter.Name,
                        Parameter     = actionParameter,
                        IsOptional    = true
                    };

                    if (actionParameter.HasCustomAttributes)
                    {
                        queryStringRoutePart.CustomAttributes = actionParameter.CustomAttributes
                                                                .Select(a => a.AttributeType.Name)
                                                                .ToList();
                    }

                    QueryStringParameters.Add(queryStringRoutePart);
                }
            }
        }