public void Create_ForSupportedType_ReturnsBinder()
    {
        // Arrange
        var provider = new ComplexTypeModelBinderProvider();

        var context = new TestModelBinderProviderContext(typeof(Person));

        context.OnCreatingBinder(m =>
        {
            if (m.ModelType == typeof(int) || m.ModelType == typeof(string))
            {
                return(Mock.Of <IModelBinder>());
            }
            else
            {
                Assert.False(true, "Not the right model type");
                return(null);
            }
        });

        // Act
        var result = provider.GetBinder(context);

        // Assert
        Assert.IsType <ComplexTypeModelBinder>(result);
    }
 public IModelBinder GetBinder(ModelBinderProviderContext context)
 {
     if (context.BindingInfo.BindingSource != null && context.BindingInfo.BindingSource.CanAcceptDataFrom(BodyOrDefaultBindingSource.BodyOrDefault))
     {
         var bodyBinder    = _bodyModelBinderProvider.GetBinder(context);
         var complexBinder = _complexTypeModelBinderProvider.GetBinder(context);
         return(new BodyOrDefaultModelBinder(bodyBinder, complexBinder));
     }
     return(null);
 }
    public void Create_ForNonComplexType_ReturnsNull(Type modelType)
    {
        // Arrange
        var provider = new ComplexTypeModelBinderProvider();

        var context = new TestModelBinderProviderContext(modelType);

        // Act
        var result = provider.GetBinder(context);

        // Assert
        Assert.Null(result);
    }
Ejemplo n.º 4
0
 public IModelBinder GetBinder(ModelBinderProviderContext context)
 {
     if (context == null)
     {
         throw new ArgumentNullException(nameof(context));
     }
     if (context.Metadata.IsComplexType)
     {
         ComplexTypeModelBinderProvider p = new ComplexTypeModelBinderProvider();
         return(new JsonMultiParameterModelBinder(p.GetBinder(context)));
     }
     return(null);
 }
        /// <summary>
        /// Creates a <see cref="T:Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinder" /> based on <see cref="T:Microsoft.AspNetCore.Mvc.ModelBinding.ModelBinderProviderContext" />.
        /// </summary>
        /// <param name="context">The <see cref="T:Microsoft.AspNetCore.Mvc.ModelBinding.ModelBinderProviderContext" />.</param>
        /// <returns>
        /// An <see cref="T:Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinder" />.
        /// </returns>
        public IModelBinder?GetBinder(ModelBinderProviderContext context)
        {
            var bodyBinder    = _bodyModelBinderProvider.GetBinder(context);
            var complexBinder = _complexTypeModelBinderProvider.GetBinder(context);

            if (context.BindingInfo.BindingSource != null &&
                context.BindingInfo.BindingSource.CanAcceptDataFrom(BodyAndRouteBindingSource.BodyAndRoute))
            {
                return(new BodyAndRouteModelBinder(bodyBinder, complexBinder));
            }
            else
            {
                return(null);
            }
        }