Example #1
0
    private static void ConfigureApiVersionsByConvention(ApiVersioningOptions options, ConventionalControllerSetting setting)
    {
        foreach (var controllerType in setting.ControllerTypes)
        {
            var controllerBuilder = options.Conventions.Controller(controllerType);

            if (setting.ApiVersions.Any())
            {
                foreach (var apiVersion in setting.ApiVersions)
                {
                    controllerBuilder.HasApiVersion(apiVersion);
                }
            }
            else
            {
                if (!controllerType.IsDefined(typeof(ApiVersionAttribute), true))
                {
                    controllerBuilder.IsApiVersionNeutral();
                }
            }
        }
    }
        private static List <string> GetSupportedVersions(Type controllerType, MethodInfo method, ConventionalControllerSetting setting)
        {
            var supportedVersions = new List <ApiVersion> ();

            var mapToAttributes = method.GetCustomAttributes <MapToApiVersionAttribute> ().ToArray();

            if (mapToAttributes.Any())
            {
                supportedVersions.AddRange(
                    mapToAttributes.SelectMany(a => a.Versions)
                    );
            }
            else
            {
                supportedVersions.AddRange(
                    controllerType.GetCustomAttributes <ApiVersionAttribute> ().SelectMany(a => a.Versions)
                    );

                setting?.ApiVersions.ForEach(supportedVersions.Add);
            }

            return(supportedVersions.Select(v => v.ToString()).Distinct().ToList());
        }
Example #3
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 = CreateAbpServiceAttributeRouteModel(rootPath, controllerName, action, httpMethod, configuration);
            }

            if (!selector.ActionConstraints.OfType <HttpMethodActionConstraint>().Any())
            {
                selector.ActionConstraints.Add(new HttpMethodActionConstraint(new[] { httpMethod }));
            }
        }
    }
Example #4
0
 protected virtual AttributeRouteModel CreateAbpServiceAttributeRouteModel(string rootPath, string controllerName, ActionModel action, string httpMethod, [CanBeNull] ConventionalControllerSetting configuration)
 {
     return(new AttributeRouteModel(
                new RouteAttribute(
                    ConventionalRouteBuilder.Build(rootPath, controllerName, action, httpMethod, configuration)
                    )
                ));
 }
Example #5
0
    protected virtual void AddAbpServiceSelector(string rootPath, string controllerName, ActionModel action, [CanBeNull] ConventionalControllerSetting configuration)
    {
        var httpMethod = SelectHttpMethod(action, configuration);

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

        action.Selectors.Add(abpServiceSelectorModel);
    }
Example #6
0
 protected virtual string SelectHttpMethod(ActionModel action, ConventionalControllerSetting configuration)
 {
     return(HttpMethodHelper.GetConventionalVerbForMethodName(action.ActionName));
 }
Example #7
0
 protected virtual void ConfigureRemoteService(ControllerModel controller, [CanBeNull] ConventionalControllerSetting configuration)
 {
     ConfigureApiExplorer(controller);
     ConfigureSelector(controller, configuration);
     ConfigureParameters(controller);
 }
Example #8
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())
        {
            AddAbpServiceSelector(rootPath, controllerName, action, configuration);
        }
        else
        {
            NormalizeSelectorRoutes(rootPath, controllerName, action, configuration);
        }
    }
        protected override string NormalizeUrlActionName(string rootPath, string controllerName, ActionModel action, string httpMethod, ConventionalControllerSetting configuration)
        {
            // return base.NormalizeUrlActionName(rootPath, controllerName, action, httpMethod, configuration);
            var actionNameInUrl =
                //HttpMethodHelper
                //.RemoveHttpMethodPrefix(action.ActionName, httpMethod)
                action.ActionName.RemovePostFix("Async");

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

            return(configuration.UrlActionNameNormalizer(
                       new UrlActionNameNormalizerContext(
                           rootPath,
                           controllerName,
                           action,
                           actionNameInUrl,
                           httpMethod
                           )
                       ));
        }
Example #10
0
        public override string Build(string rootPath, string controllerName, ActionModel action, string httpMethod, ConventionalControllerSetting configuration)
        {
            var controllerNameInUrl = NormalizeUrlControllerName(rootPath, controllerName, action, httpMethod, configuration);

            var url = $"api/{rootPath}/{NormalizeControllerNameCase(controllerNameInUrl, configuration)}";

            //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 += "/{" + NormalizeIdPropertyNameCase(property, configuration) + "}";
            //         }
            //     }
            // }

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

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

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

            return(url);
        }
Example #11
0
 protected override void ConfigureSelector(string rootPath, string controllerName, ActionModel action, ConventionalControllerSetting configuration)
 {
     base.ConfigureSelector(rootPath, controllerName, action, configuration);
 }
Example #12
0
 protected virtual string NormalizeSecondaryIdNameCase(ParameterModel secondaryId, [CanBeNull] ConventionalControllerSetting configuration)
 {
     return(secondaryId.ParameterName);
 }
Example #13
0
 protected virtual string NormalizeIdPropertyNameCase(PropertyInfo property, [CanBeNull] ConventionalControllerSetting configuration)
 {
     return(property.Name);
 }