Пример #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(new 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(methodInfo =>
                                                                       action.ActionMethod.Name == methodInfo.Name &&
                                                                       !actionParams.Except(methodInfo.GetParameters(), new ModelConventionHelper.ParameterInfoEqualityComparer()).Any());

            if (method == null)
            {
                return;
            }

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

            if (!attrs.Any(x => x is HttpMethodAttribute || x is RouteAttribute))
            {
                if (actionParams.Any(x => !(AppManager.IsValueType(x.ParameterType) || x.ParameterType.IsEnum)) ||
                    method.Name.StartsWith("Delete") || method.Name.StartsWith("Update"))
                {
                    actionAttrs.Add(new HttpPost(routePath));
                }
                else
                {
                    actionAttrs.Add(new HttpGet(routePath));
                }
            }
            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));
        }