Пример #1
0
        public IDictionary <string, object> ParserRouteParameters(string path)
        {
            var routeParameters = new Dictionary <string, object>();
            var api             = TrimPrefix(path);
            var apiSegments     = api.Split(separator);

            for (var index = 0; index < apiSegments.Length; index++)
            {
                var apiSegment   = apiSegments[index];
                var routeSegment = RouteTemplate.Segments[index];
                if (!routeSegment.IsParameter)
                {
                    continue;
                }

                routeParameters.Add(TemplateSegmentHelper.GetVariableName(routeSegment.Value), apiSegment);
            }

            return(routeParameters);
        }
Пример #2
0
        private static ParameterDescriptor CreateParameterDescriptor(MethodInfo methodInfo, ParameterInfo parameter,
                                                                     HttpMethod httpMethod)
        {
            var bindingSourceMetadata =
                parameter.GetCustomAttributes().OfType <IBindingSourceMetadata>().FirstOrDefault();
            ParameterDescriptor parameterDescriptor = null;

            if (bindingSourceMetadata != null)
            {
                var parameterFrom = bindingSourceMetadata.BindingSource.Id.To <ParameterFrom>();
                if (httpMethod == HttpMethod.Get && parameterFrom == ParameterFrom.Body)
                {
                    throw new LmsException("Get请求不允许通过RequestBody获取参数值");
                }

                if (parameterFrom == ParameterFrom.Path && !parameter.ParameterType.IsSample())
                {
                    throw new LmsException($"路由类型参数不允许为复杂数据类型");
                }

                parameterDescriptor = new ParameterDescriptor(parameterFrom, parameter);
            }
            else
            {
                if (parameter.IsSampleType())
                {
                    var httpMethodAttribute =
                        methodInfo.GetCustomAttributes().OfType <HttpMethodAttribute>().FirstOrDefault(p =>
                                                                                                       p.HttpMethods.Contains(httpMethod.ToString().ToUpper()));
                    if (httpMethodAttribute == null)
                    {
                        parameterDescriptor =
                            new ParameterDescriptor(ParameterFrom.Query, parameter);
                    }
                    else
                    {
                        var routeTemplate         = httpMethodAttribute.Template;
                        var routeTemplateSegments = routeTemplate.Split("/");
                        var parameterFromPath     = false;
                        foreach (var routeTemplateSegment in routeTemplateSegments)
                        {
                            if (TemplateSegmentHelper.IsVariable(routeTemplateSegment) &&
                                TemplateSegmentHelper.GetVariableName(routeTemplateSegment) == parameter.Name)
                            {
                                parameterDescriptor =
                                    new ParameterDescriptor(ParameterFrom.Path, parameter, TemplateSegmentHelper.GetSegmentVal(routeTemplateSegment));
                                parameterFromPath = true;
                                break;
                            }
                        }

                        if (!parameterFromPath)
                        {
                            parameterDescriptor =
                                new ParameterDescriptor(ParameterFrom.Query, parameter);
                        }
                    }
                }
                else
                {
                    parameterDescriptor =
                        new ParameterDescriptor(ParameterFrom.Body, parameter);
                }
            }

            return(parameterDescriptor);
        }
Пример #3
0
        public object[] ResolveParameters(IDictionary <ParameterFrom, object> parameters)
        {
            var list = new List <object>();
            var typeConvertibleService = EngineContext.Current.Resolve <ITypeConvertibleService>();

            foreach (var parameterDescriptor in ParameterDescriptors)
            {
                #region 获取参数

                var parameter = parameterDescriptor.From.DefaultValue();
                if (parameters.ContainsKey(parameterDescriptor.From))
                {
                    parameter = parameters[parameterDescriptor.From];
                }

                switch (parameterDescriptor.From)
                {
                case ParameterFrom.Body:
                    list.Add(parameter);
                    break;

                case ParameterFrom.Form:
                    if (parameterDescriptor.IsSample)
                    {
                        SetSampleParameterValue(typeConvertibleService, parameter, parameterDescriptor, list);
                    }
                    else
                    {
                        list.Add(parameter);
                    }

                    break;

                case ParameterFrom.Header:
                    if (parameterDescriptor.IsSample)
                    {
                        SetSampleParameterValue(typeConvertibleService, parameter, parameterDescriptor, list);
                    }
                    else
                    {
                        list.Add(parameter);
                    }

                    break;

                case ParameterFrom.Path:
                    if (parameterDescriptor.IsSample)
                    {
                        var pathVal =
                            (IDictionary <string, object>)typeConvertibleService.Convert(parameter,
                                                                                         typeof(IDictionary <string, object>));
                        var parameterName = TemplateSegmentHelper.GetVariableName(parameterDescriptor.Name);
                        if (!pathVal.ContainsKey(parameterName))
                        {
                            throw new LmsException("path参数不允许为空,请确认您传递的参数是否正确");
                        }

                        var parameterVal = pathVal[parameterName];
                        list.Add(typeConvertibleService.Convert(parameterVal, parameterDescriptor.Type));
                    }
                    else
                    {
                        throw new LmsException("复杂数据类型不支持通过路由模板进行获取");
                    }

                    break;

                case ParameterFrom.Query:
                    if (parameterDescriptor.IsSample)
                    {
                        SetSampleParameterValue(typeConvertibleService, parameter, parameterDescriptor, list);
                    }
                    else
                    {
                        list.Add(parameter);
                    }

                    break;
                }

                #endregion
            }

            return(list.ToArray());
        }