public void Apply(ActionModel action)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            if (IsConventionApplicable(action.Controller))
            {
                foreach (var parameter in action.Parameters)
                {
                    // Some IBindingSourceMetadata attributes like ModelBinder attribute return null
                    // as their binding source. Special case to ensure we do not ignore them.
                    if (parameter.BindingInfo?.BindingSource != null ||
                        parameter.Attributes.OfType <IBindingSourceMetadata>().Any())
                    {
                        // This has a binding behavior configured, just leave it alone.
                    }
                    else if (IsHttpGetMethod(action) || CanConvertFromString(parameter.ParameterInfo.ParameterType))
                    {
                        // Simple types are by-default from the URI.
                        parameter.BindingInfo = parameter.BindingInfo ?? new BindingInfo();
                        parameter.BindingInfo.BindingSource = CompositeBindingSource.Create(new BindingSource[]
                        {
                            BindingSource.Query,
                            BindingSource.Path
                        }, "Uri");
                    }
                    else
                    {
                        // Complex types are by-default from the body.
                        parameter.BindingInfo = parameter.BindingInfo ?? new BindingInfo();
                        parameter.BindingInfo.BindingSource = BindingSource.Body;
                    }

                    //var optionalParameters = new HashSet<string>();
                    //// For all non IOptionalBinderMetadata, which are not URL source (like FromQuery etc.) do not
                    //// participate in overload selection and hence are added to the hashset so that they can be
                    //// ignored in OverloadActionConstraint.
                    //var optionalMetadata = parameter.Attributes.OfType<IBindingSourceMetadata>().SingleOrDefault();
                    //if ((parameter.ParameterInfo.HasDefaultValue && parameter.BindingInfo.BindingSource == uriBindingSource) ||
                    //    optionalMetadata == null && parameter.BindingInfo.BindingSource != uriBindingSource)
                    //{
                    //    String name = (optionalMetadata as IModelNameProvider)?.Name;
                    //    parameter.ParameterName = name.IfNullOrWhiteSpace(parameter.ParameterName);
                    //    optionalParameters.Add(name.IfNullOrWhiteSpace(parameter.ParameterName));
                    //}
                }

                //action.Properties.Add("OptionalParameters", optionalParameters);
            }
        }
    public void CompositeBindingSourceTest_CanAcceptDataFrom_NoMatch()
    {
        // Arrange
        var composite = CompositeBindingSource.Create(
            bindingSources: new BindingSource[] { BindingSource.Query, BindingSource.Form },
            displayName: "Test Source1");

        // Act
        var result = composite.CanAcceptDataFrom(BindingSource.Path);

        // Assert
        Assert.False(result);
    }
    public void BindingSourceValueProvider_ThrowsOnCompositeSource()
    {
        // Arrange
        var expected = $"The provided binding source 'Test Source' is a composite. '{nameof(BindingSourceValueProvider)}' " +
                       "requires that the source must represent a single type of input.";

        var bindingSource = CompositeBindingSource.Create(
            bindingSources: new BindingSource[] { BindingSource.Query, BindingSource.Form },
            displayName: "Test Source");

        // Act & Assert
        ExceptionAssert.ThrowsArgument(
            () => new TestableBindingSourceValueProvider(bindingSource),
            "bindingSource",
            expected);
    }
Esempio n. 4
0
    public void BindingSource_CanAcceptDataFrom_ThrowsOnComposite()
    {
        // Arrange
        var expected = "The provided binding source 'Test Source' is a composite. " +
                       $"'{nameof(BindingSource.CanAcceptDataFrom)}' requires that the source must represent a single type of input.";

        var bindingSource = CompositeBindingSource.Create(
            bindingSources: new BindingSource[] { BindingSource.Query, BindingSource.Form },
            displayName: "Test Source");

        // Act & Assert
        ExceptionAssert.ThrowsArgument(
            () => BindingSource.Query.CanAcceptDataFrom(bindingSource),
            "bindingSource",
            expected);
    }