Пример #1
0
        public void Apply(ControllerModel controller)
        {
            if (!serviceType.IsAssignableFrom(controller.ControllerType))
            {
                return;
            }

            var attrs           = serviceType.GetCustomAttributes();
            var controllerAttrs = new List <object>();

            foreach (var att in attrs)
            {
                if (att is RouteAttribute routeAttr)
                {
                    var template = routeAttr.Template;
                    controllerAttrs.Add(Activator.CreateInstance(typeof(Route), template));
                }
            }

            if (controllerAttrs.Any())
            {
                controller.Selectors.Clear();
                ModelConventionHelper.AddRange(controller.Selectors, ModelConventionHelper.CreateSelectors(controllerAttrs));
            }
        }
Пример #2
0
        public void Apply(ActionModel action)
        {
            if (!serviceType.IsAssignableFrom(action.Controller.ControllerType))
            {
                return;
            }

            var actionParams = action.ActionMethod.GetParameters();

            var method = serviceType.GetMethods().FirstOrDefault(mth => action.ActionMethod.Name == mth.Name && !actionParams.Except(mth.GetParameters(), new ModelConventionHelper.ParameterInfoEqualityComparer()).Any());

            if (method == null)
            {
                return;
            }

            var attrs       = method.GetCustomAttributes();
            var actionAttrs = new List <object>();

            if (!attrs.Any(x => x is HttpMethodAttribute || x is RouteAttribute))
            {
                actionAttrs.Add(new HttpPost(method.GetPath()));
            }
            else
            {
                foreach (var att in attrs)
                {
                    switch (att)
                    {
                    case HttpMethodAttribute methodAttr:
                        var httpMethod = methodAttr.Method;
                        var path       = methodAttr.Path;

                        if (httpMethod == HttpMethod.Get)
                        {
                            actionAttrs.Add(new HttpGet(path));
                        }
                        else if (httpMethod == HttpMethod.Post)
                        {
                            actionAttrs.Add(new HttpPost(path));
                        }
                        else if (httpMethod == HttpMethod.Put)
                        {
                            actionAttrs.Add(new HttpPut(path));
                        }
                        else if (httpMethod == HttpMethod.Delete)
                        {
                            actionAttrs.Add(new HttpDelete(path));
                        }
                        else if (httpMethod == HttpMethod.Head)
                        {
                            actionAttrs.Add(new HttpHead(path));
                        }
                        else if (httpMethod == HttpMethod.Options)
                        {
                            actionAttrs.Add(new HttpOptions(path));
                        }
                        break;

                    case RouteAttribute routeAttr:
                        actionAttrs.Add(new Route(routeAttr.Template));
                        break;
                    }
                }
            }

            action.Selectors.Clear();
            ModelConventionHelper.AddRange(action.Selectors, ModelConventionHelper.CreateSelectors(actionAttrs));
        }
Пример #3
0
        public void Apply(ActionModel action)
        {
            if (!serviceType.IsAssignableFrom(action.Controller.ControllerType))
            {
                return;
            }

            var actionParams = action.ActionMethod.GetParameters();

            var method = serviceType.GetMethods().FirstOrDefault(mth =>
            {
                var mthParams = mth.GetParameters();
                return(action.ActionMethod.Name == mth.Name &&
                       actionParams.Length == mthParams.Length &&
                       actionParams.Any(x => mthParams.Where(o => x.Name == o.Name).Any(o => x.GetType() == o.GetType())));
            });

            if (method == null)
            {
                return;
            }

            var attrs       = method.GetCustomAttributes();
            var actionAttrs = new List <object>();

            if (!attrs.Any(x => x is HttpMethodAttribute || x is RouteAttribute))
            {
                actionAttrs.Add(new HttpPost($"method/{method.Name}/{string.Join("-", method.GetParameters().Select(x => x.Name))}"));
            }
            else
            {
                foreach (var att in attrs)
                {
                    switch (att)
                    {
                    case HttpMethodAttribute methodAttr:
                        var httpMethod = methodAttr.Method;
                        var path       = methodAttr.Path;

                        if (httpMethod == HttpMethod.Get)
                        {
                            actionAttrs.Add(new HttpGet(path));
                        }
                        else if (httpMethod == HttpMethod.Post)
                        {
                            actionAttrs.Add(new HttpPost(path));
                        }
                        else if (httpMethod == HttpMethod.Put)
                        {
                            actionAttrs.Add(new HttpPut(path));
                        }
                        else if (httpMethod == HttpMethod.Delete)
                        {
                            actionAttrs.Add(new HttpDelete(path));
                        }
                        else if (httpMethod == HttpMethod.Head)
                        {
                            actionAttrs.Add(new HttpHead(path));
                        }
                        else if (httpMethod == HttpMethod.Options)
                        {
                            actionAttrs.Add(new HttpOptions(path));
                        }
                        break;

                    case RouteAttribute routeAttr:
                        actionAttrs.Add(new Route(routeAttr.Template));
                        break;
                    }
                }
            }

            action.Selectors.Clear();
            ModelConventionHelper.AddRange(action.Selectors, ModelConventionHelper.CreateSelectors(actionAttrs));
        }
Пример #4
0
        public void Apply(ActionModel action)
        {
            if (!serviceType.IsAssignableFrom(action.Controller.ControllerType))
            {
                return;
            }

            var actionParams = action.ActionMethod.GetParameters();

            var method = serviceType.GetMethods().FirstOrDefault(mth =>
            {
                var mthParams = mth.GetParameters();
                return(action.ActionMethod.Name == mth.Name &&
                       actionParams.Length == mthParams.Length &&
                       actionParams.Any(x => mthParams.Where(o => x.Name == o.Name).Any(o => x.GetType() == o.GetType())));
            });

            var attrs       = method.GetCustomAttributes();
            var actionAttrs = new List <object>();

            foreach (var att in attrs)
            {
                if (att is HttpMethodAttribute methodAttr)
                {
                    var httpMethod = methodAttr.Method;
                    var path       = methodAttr.Path;

                    if (httpMethod == HttpMethod.Get)
                    {
                        actionAttrs.Add(Activator.CreateInstance(typeof(HttpGet), path));
                    }
                    else if (httpMethod == HttpMethod.Post)
                    {
                        actionAttrs.Add(Activator.CreateInstance(typeof(HttpPost), path));
                    }
                    else if (httpMethod == HttpMethod.Put)
                    {
                        actionAttrs.Add(Activator.CreateInstance(typeof(HttpPut), path));
                    }
                    else if (httpMethod == HttpMethod.Delete)
                    {
                        actionAttrs.Add(Activator.CreateInstance(typeof(HttpDelete), path));
                    }
                    else if (httpMethod == HttpMethod.Head)
                    {
                        actionAttrs.Add(Activator.CreateInstance(typeof(HttpHead), path));
                    }
                    else if (httpMethod == HttpMethod.Options)
                    {
                        actionAttrs.Add(Activator.CreateInstance(typeof(HttpOptions), path));
                    }
                }
                if (att is RouteAttribute routeAttr)
                {
                    actionAttrs.Add(Activator.CreateInstance(typeof(Route), routeAttr.Template));
                }
            }

            if (actionAttrs.Any())
            {
                action.Selectors.Clear();
                ModelConventionHelper.AddRange(action.Selectors, ModelConventionHelper.CreateSelectors(actionAttrs));
            }
        }