Ejemplo n.º 1
0
        /// <summary>
        /// Gets or creates the convention builder for the specified controller action method.
        /// </summary>
        /// <param name="builder">The extended <see cref="IActionConventionBuilder"/>.</param>
        /// <param name="methodName">The name of the action method.</param>
        /// <param name="argumentTypes">The optional array of action method argument types.</param>
        /// <returns>A new or existing <see cref="ActionApiVersionConventionBuilder"/>.</returns>
        /// <remarks>The specified <paramref name="methodName">method name</paramref> must refer to a public, non-static action method.
        /// If there is only one corresponding match found, then the <paramref name="argumentTypes">argument types</paramref> are ignored;
        /// otherwise, the <paramref name="argumentTypes">argument types</paramref> are used for method overload resolution. Action
        /// methods that have the <see cref="NonActionAttribute"/> applied will also be ignored.</remarks>
        public static ActionApiVersionConventionBuilder Action(this IActionConventionBuilder builder, string methodName, params Type[] argumentTypes)
        {
            Arg.NotNull(builder, nameof(builder));

            var methods = builder.ControllerType.GetRuntimeMethods().Where(m => m.Name == methodName && IsAction(m)).ToArray();

            switch (methods.Length)
            {
            case 0:
                throw new MissingMethodException(SR.ActionMethodNotFound.FormatDefault(methodName));

            case 1:
                return(builder.Action(methods[0]));
            }

            argumentTypes = argumentTypes ?? Type.EmptyTypes;
            methods       = methods.Where(m => SignatureMatches(m, argumentTypes)).ToArray();

            if (methods.Length == 1)
            {
                return(builder.Action(methods[0]));
            }

            throw new AmbiguousMatchException(SR.AmbiguousActionMethod.FormatDefault(methodName));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets or creates the convention builder for the specified controller action method.
        /// </summary>
        /// <param name="builder">The extended <see cref="ActionApiVersionConventionBuilder"/>.</param>
        /// <param name="methodName">The name of the action method.</param>
        /// <param name="argumentTypes">The optional array of action method argument types.</param>
        /// <returns>A new or existing <see cref="ActionApiVersionConventionBuilder"/>.</returns>
        /// <remarks>The specified <paramref name="methodName">method name</paramref> must refer to a public, non-static action method.
        /// If there is only one corresponding match found, then the <paramref name="argumentTypes">argument types</paramref> are ignored;
        /// otherwise, the <paramref name="argumentTypes">argument types</paramref> are used for method overload resolution. Action
        /// methods that have the <see cref="NonActionAttribute"/> applied will also be ignored.</remarks>
        public static IActionConventionBuilder Action(this IActionConventionBuilder builder, string methodName, params Type[] argumentTypes)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            var method = ActionMethodResolver.Resolve(builder.ControllerType, methodName, argumentTypes);

            return(builder.Action(method));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets or creates the convention builder for the specified controller action method.
        /// </summary>
        /// <typeparam name="TController">The type of controller.</typeparam>
        /// <param name="builder">The extended <see cref="IActionConventionBuilder{T}"/>.</param>
        /// <param name="actionExpression">The <see cref="Expression{TDelegate}">expression</see> representing the controller action method.</param>
        /// <returns>A new or existing <see cref="IActionConventionBuilder{T}"/>.</returns>
        public static IActionConventionBuilder <TController> Action <TController>(this IActionConventionBuilder <TController> builder, Expression <Action <TController> > actionExpression)
            where TController : notnull
#if WEBAPI
#pragma warning disable SA1001 // Commas should be spaced correctly
        , IHttpController
#pragma warning restore SA1001 // Commas should be spaced correctly
#endif
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

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

            return(builder.Action(actionExpression.ExtractMethod()));
        }
        /// <summary>
        /// Gets or creates the convention builder for the specified controller action method.
        /// </summary>
        /// <typeparam name="TController">The type of controller.</typeparam>
        /// <typeparam name="TResult">The type of action result.</typeparam>
        /// <param name="builder">The extended <see cref="IActionConventionBuilder{T}"/>.</param>
        /// <param name="actionExpression">The <see cref="Expression{TDelegate}">expression</see> representing the controller action method</param>
        /// <returns>A new or existing <see cref="ActionApiVersionConventionBuilder{T}"/>.</returns>
        public static ActionApiVersionConventionBuilder <TController> Action <TController, TResult>(this IActionConventionBuilder <TController> builder, Expression <Func <TController, TResult> > actionExpression)
#if WEBAPI
            where TController : IHttpController
#endif
        {
            Arg.NotNull(builder, nameof(builder));
            return(builder.Action(actionExpression.ExtractMethod()));
        }