Example #1
0
        protected virtual string NormalizeUrlControllerName(string rootPath, string controllerName, ActionModel action, string httpMethod, [CanBeNull] ConventionalControllerSetting configuration)
        {
            if (configuration?.UrlControllerNameNormalizer == null)
            {
                return(controllerName);
            }

            return(configuration.UrlControllerNameNormalizer(
                       new UrlControllerNameNormalizerContext(
                           rootPath,
                           controllerName
                           )
                       ));
        }
Example #2
0
 protected virtual void ConfigureRemoteService(ControllerModel controller, [CanBeNull] ConventionalControllerSetting configuration)
 {
     ConfigureApiExplorer(controller);
     ConfigureSelector(controller, configuration);
     ConfigureParameters(controller);
 }
Example #3
0
        protected virtual string CalculateRouteTemplate(string rootPath, string controllerName, ActionModel action, string httpMethod, [CanBeNull] ConventionalControllerSetting configuration)
        {
            var controllerNameInUrl = NormalizeUrlControllerName(rootPath, controllerName, action, httpMethod, configuration);

            var url = $"api/{rootPath}/{controllerNameInUrl.ToCamelCase()}";

            //Add {id} path if needed
            var idParameterModel = action.Parameters.FirstOrDefault(p => p.ParameterName == "id");

            if (idParameterModel != null)
            {
                if (TypeHelper.IsPrimitiveExtended(idParameterModel.ParameterType, includeEnums: true))
                {
                    url += "/{id}";
                }
                else
                {
                    var properties = idParameterModel
                                     .ParameterType
                                     .GetProperties(BindingFlags.Instance | BindingFlags.Public);

                    foreach (var property in properties)
                    {
                        url += "/{" + property.Name + "}";
                    }
                }
            }

            //Add action name if needed
            var actionNameInUrl = NormalizeUrlActionName(rootPath, controllerName, action, httpMethod, configuration);

            if (!actionNameInUrl.IsNullOrEmpty())
            {
                url += $"/{actionNameInUrl.ToCamelCase()}";

                //Add secondary Id
                var secondaryIds = action.Parameters.Where(p => p.ParameterName.EndsWith("Id", StringComparison.Ordinal)).ToList();
                if (secondaryIds.Count == 1)
                {
                    url += $"/{{{secondaryIds[0].ParameterName}}}";
                }
            }

            return(url);
        }
Example #4
0
        protected virtual string NormalizeUrlActionName(string rootPath, string controllerName, ActionModel action, string httpMethod, [CanBeNull] ConventionalControllerSetting configuration)
        {
            var actionNameInUrl = HttpMethodHelper
                                  .RemoveHttpMethodPrefix(action.ActionName, httpMethod)
                                  .RemovePostFix("Async");

            if (configuration?.UrlActionNameNormalizer == null)
            {
                return(actionNameInUrl);
            }

            return(configuration.UrlActionNameNormalizer(
                       new UrlActionNameNormalizerContext(
                           rootPath,
                           controllerName,
                           action,
                           actionNameInUrl,
                           httpMethod
                           )
                       ));
        }
Example #5
0
 protected virtual AttributeRouteModel CreateRocketServiceAttributeRouteModel(string rootPath, string controllerName, ActionModel action, string httpMethod, [CanBeNull] ConventionalControllerSetting configuration)
 {
     return(new AttributeRouteModel(
                new RouteAttribute(
                    CalculateRouteTemplate(rootPath, controllerName, action, httpMethod, configuration)
                    )
                ));
 }
Example #6
0
        protected virtual void NormalizeSelectorRoutes(string rootPath, string controllerName, ActionModel action, [CanBeNull] ConventionalControllerSetting configuration)
        {
            foreach (var selector in action.Selectors)
            {
                var httpMethod = selector.ActionConstraints
                                 .OfType <HttpMethodActionConstraint> ()
                                 .FirstOrDefault() ?
                                 .HttpMethods?
                                 .FirstOrDefault();

                if (httpMethod == null)
                {
                    httpMethod = SelectHttpMethod(action, configuration);
                }

                if (selector.AttributeRouteModel == null)
                {
                    selector.AttributeRouteModel = CreateRocketServiceAttributeRouteModel(rootPath, controllerName, action, httpMethod, configuration);
                }

                if (!selector.ActionConstraints.OfType <HttpMethodActionConstraint> ().Any())
                {
                    selector.ActionConstraints.Add(new HttpMethodActionConstraint(new [] { httpMethod }));
                }
            }
        }
Example #7
0
 protected virtual string SelectHttpMethod(ActionModel action, ConventionalControllerSetting configuration)
 {
     return(HttpMethodHelper.GetConventionalVerbForMethodName(action.ActionName));
 }
Example #8
0
        protected virtual void AddRocketServiceSelector(string rootPath, string controllerName, ActionModel action, [CanBeNull] ConventionalControllerSetting configuration)
        {
            var httpMethod = SelectHttpMethod(action, configuration);

            var rocketServiceSelectorModel = new SelectorModel {
                AttributeRouteModel = CreateRocketServiceAttributeRouteModel(rootPath, controllerName, action, httpMethod, configuration),
                ActionConstraints   = { new HttpMethodActionConstraint(new [] { httpMethod }) }
            };

            action.Selectors.Add(rocketServiceSelectorModel);
        }
Example #9
0
        protected virtual void ConfigureSelector(string rootPath, string controllerName, ActionModel action, [CanBeNull] ConventionalControllerSetting configuration)
        {
            RemoveEmptySelectors(action.Selectors);

            var remoteServiceAtt = ReflectionHelper.GetSingleAttributeOrDefault <RemoteServiceAttribute> (action.ActionMethod);

            if (remoteServiceAtt != null && !remoteServiceAtt.IsEnabledFor(action.ActionMethod))
            {
                return;
            }

            if (!action.Selectors.Any())
            {
                AddRocketServiceSelector(rootPath, controllerName, action, configuration);
            }
            else
            {
                NormalizeSelectorRoutes(rootPath, controllerName, action, configuration);
            }
        }