コード例 #1
0
        /// <summary>
        /// Applies a controller convention given the specified builder and model.
        /// </summary>
        /// <param name="controller">The <see cref="IControllerConventionBuilder">builder</see> used to apply conventions.</param>
        /// <param name="controllerDescriptor">The <see cref="HttpControllerDescriptor">descriptor</see> to build conventions from.</param>
        /// <returns>True if any conventions were applied to the <paramref name="controllerDescriptor">descriptor</paramref>;
        /// otherwise, false.</returns>
        public virtual bool Apply(IControllerConventionBuilder controller, HttpControllerDescriptor controllerDescriptor)
        {
            Arg.NotNull(controller, nameof(controller));
            Arg.NotNull(controllerDescriptor, nameof(controller));

            var text = GetRawApiVersion(controllerDescriptor.ControllerType.Namespace);

            if (!ApiVersion.TryParse(text, out var apiVersion))
            {
                return(false);
            }

            var deprecated = controllerDescriptor.GetCustomAttributes <ObsoleteAttribute>().Any();

            if (deprecated)
            {
                controller.HasDeprecatedApiVersion(apiVersion);
            }
            else
            {
                controller.HasApiVersion(apiVersion);
            }

            return(true);
        }
        /// <summary>
        /// Applies a controller convention given the specified builder and model.
        /// </summary>
        /// <param name="controller">The <see cref="IControllerConventionBuilder">builder</see> used to apply conventions.</param>
        /// <param name="controllerModel">The <see cref="ControllerModel">model</see> to build conventions from.</param>
        /// <returns>True if any conventions were applied to the <paramref name="controllerModel">controller model</paramref>;
        /// otherwise, false.</returns>
        public virtual bool Apply(IControllerConventionBuilder controller, ControllerModel controllerModel)
        {
            if (controller == null)
            {
                throw new ArgumentNullException(nameof(controller));
            }

            if (controllerModel == null)
            {
                throw new ArgumentNullException(nameof(controllerModel));
            }

            if (GetApiVersion(controllerModel.ControllerType.Namespace !) is not ApiVersion apiVersion)
            {
                return(false);
            }

            var deprecated = controllerModel.Attributes.OfType <ObsoleteAttribute>().Any();

            if (deprecated)
            {
                controller.HasDeprecatedApiVersion(apiVersion);
            }
            else
            {
                controller.HasApiVersion(apiVersion);
            }

            return(true);
        }
        /// <summary>
        /// Applies a controller convention given the specified builder and model.
        /// </summary>
        /// <param name="controller">The <see cref="IControllerConventionBuilder">builder</see> used to apply conventions.</param>
        /// <param name="controllerDescriptor">The <see cref="HttpControllerDescriptor">descriptor</see> to build conventions from.</param>
        /// <returns>True if any conventions were applied to the <paramref name="controllerDescriptor">descriptor</paramref>;
        /// otherwise, false.</returns>
        public virtual bool Apply(IControllerConventionBuilder controller, HttpControllerDescriptor controllerDescriptor)
        {
            if (controller == null)
            {
                throw new ArgumentNullException(nameof(controller));
            }

            if (controllerDescriptor == null)
            {
                throw new ArgumentNullException(nameof(controllerDescriptor));
            }

            if (GetApiVersion(controllerDescriptor.ControllerType.Namespace) is not ApiVersion apiVersion)
            {
                return(false);
            }

            var deprecated = controllerDescriptor.GetCustomAttributes <ObsoleteAttribute>().Any();

            if (deprecated)
            {
                controller.HasDeprecatedApiVersion(apiVersion);
            }
            else
            {
                controller.HasApiVersion(apiVersion);
            }

            return(true);
        }