コード例 #1
0
        private IEnumerable <OperationInfo> BuildMethodDescriptor(MethodInfo method, RouteAttribute prefix)
        {
            bool explicitRoute;
            var  verbs      = new List <OnVerbAttribute>();
            var  route      = method.GetDefaults(verbs, out explicitRoute);
            Type entityType = null;
            Type implementation;

            if ((!explicitRoute) && ((implementation = typeof(T).GetInterfaces().FirstOrDefault(@interface => (@interface.IsGenericType) && (typeof(IReadController <,>).IsAssignableFrom(@interface.GetGenericTypeDefinition())))) != null))
            {
                entityType = implementation.GetGenericArguments()[0];
            }

            UriTemplateBuilder templateRegex = new UriTemplateBuilder(entityType, true);

            templateRegex.Add(prefix.Url.ToString(), prefix, typeof(T));
            templateRegex.Add(route.Url.ToString(), route, method);
            Url url = UrlParser.Parse(templateRegex.ToString());
            IList <OperationInfo> result = new List <OperationInfo>();

            foreach (var item in verbs)
            {
                UriTemplateBuilder uriTemplate = templateRegex.Clone(false);
                var parameters = BuildParameterDescriptors(method, item.Verb, templateRegex, ref uriTemplate);
                var regex      = new Regex("^" + templateRegex + "$", RegexOptions.IgnoreCase);
                result.Add(new OperationInfo <Verb>(method, url, uriTemplate, regex, item.Verb, parameters).WithSecurityDetailsFrom(method));
            }

            return(result);
        }
コード例 #2
0
        private ValueInfo[] BuildParameterDescriptors(MethodInfo method, Verb verb, UriTemplateBuilder templateRegex, ref UriTemplateBuilder uriTemplate)
        {
            IList <ValueInfo> result = new List <ValueInfo>();

            if (method.ReturnParameter.ParameterType != typeof(void))
            {
                result.Add(new ResultInfo(method.ReturnParameter, CreateResultTarget(method.ReturnParameter), null, null));
            }

            foreach (var parameter in method.GetParameters())
            {
                if (parameter.IsOut)
                {
                    result.Add(new ResultInfo(parameter, CreateResultTarget(parameter), null, null));
                    continue;
                }

                bool   isBodyParameter;
                string parameterUrlTemplate;
                string parameterTemplateRegex;
                var    source = CreateParameterTemplateRegex(method, parameter, verb, out parameterUrlTemplate, out parameterTemplateRegex, out isBodyParameter);
                if ((parameterTemplateRegex == null) && (!isBodyParameter))
                {
                    continue;
                }

                string variableName = (isBodyParameter ? null : UriTemplateBuilder.VariableTemplateRegex.Match(parameterUrlTemplate).Groups["ParameterName"].Value);
                if (parameterTemplateRegex != null)
                {
                    if (!(source is FromQueryStringAttribute))
                    {
                        templateRegex.Add(parameterTemplateRegex, source, parameter, parameter.HasDefaultValue);
                    }

                    uriTemplate.Add(parameterUrlTemplate, source, parameter, parameter.HasDefaultValue);
                }

                result.Add(new ArgumentInfo(parameter, source, (isBodyParameter ? null : (parameterTemplateRegex.StartsWith("([?&]") ? parameterUrlTemplate : uriTemplate.ToString(false))), variableName));
            }

            return(result.ToArray());
        }