private static bool TryGetProviderFromAttributes(Type modelType, out ModelBinderProvider provider) { ModelBinderAttribute attr = TypeDescriptorHelper.Get(modelType).GetAttributes().OfType <ModelBinderAttribute>().FirstOrDefault(); if (attr == null) { provider = null; return(false); } // TODO, 386718, remove the following if statement when the bug is resolved if (attr.BinderType == null) { provider = null; return(false); } if (typeof(ModelBinderProvider).IsAssignableFrom(attr.BinderType)) { provider = (ModelBinderProvider)Activator.CreateInstance(attr.BinderType); } else { throw Error.InvalidOperation(SRResources.ModelBinderProviderCollection_InvalidBinderType, attr.BinderType.Name, typeof(ModelBinderProvider).Name, typeof(IModelBinder).Name); } return(true); }
static IModelBinder GetModelBinder(IFromRouteAttribute fromRouteAttr, Type modelType, HttpConfiguration configuration) { modelType = TypeHelpers.GetNullableUnderlyingType(modelType); ModelBinderAttribute modelBinderAttr = null; if (fromRouteAttr != null) { modelBinderAttr = (ModelBinderAttribute)fromRouteAttr; } if (modelBinderAttr == null || modelBinderAttr.BinderType == null) { ModelBinderAttribute modelTypeAttr = modelBinderAttrCache.GetOrAdd(modelType, t => t.GetCustomAttributes(typeof(ModelBinderAttribute), inherit: true) .Cast <ModelBinderAttribute>() .SingleOrDefault() ); if (modelTypeAttr != null && (modelBinderAttr == null || modelTypeAttr.BinderType != null)) { modelBinderAttr = modelTypeAttr; } } if (modelBinderAttr == null) { modelBinderAttr = new ModelBinderAttribute(); } return(modelBinderAttr.GetModelBinder(configuration, modelType)); }
/// <summary> /// Bind the parameter using default model binding but with the supplied value providers. /// </summary> /// <param name="parameter">parameter to provide binding for.</param> /// <param name="valueProviderFactories">value provider factories to feed to model binders</param> /// <returns>a binding</returns> public static HttpParameterBinding BindWithModelBinding(this HttpParameterDescriptor parameter, IEnumerable <ValueProviderFactory> valueProviderFactories) { HttpConfiguration config = parameter.Configuration; IModelBinder binder = new ModelBinderAttribute().GetModelBinder(config, parameter.ParameterType); return(new ModelBinderParameterBinding(parameter, binder, valueProviderFactories)); }
internal static bool IsChangeSetEntityParameter(HttpParameterDescriptor parameterDescriptor) { Contract.Assert(parameterDescriptor != null); ModelBinderAttribute modelBinderAttr = parameterDescriptor.ParameterBinderAttribute as ModelBinderAttribute; return((modelBinderAttr != null) && (modelBinderAttr.BinderType == typeof(ChangeSetEntityModelBinder))); }
public void BinderTypeProperty() { // Arrange Type binderType = typeof(GoodConverter); ModelBinderAttribute attr = new ModelBinderAttribute(binderType); // Act & Assert Assert.Same(binderType, attr.BinderType); }
public override HttpParameterBinding GetBinding(HttpParameterDescriptor parameter) { var httpConfig = parameter.Configuration; var binder = new ModelBinderAttribute() .GetModelBinder(httpConfig, parameter.ParameterType); return(new ModelBinderParameterBinding( parameter, binder, new ValueProviderFactory[] { new HeaderValueProviderFactory() })); }
public void BindWithModelBinding_Attribute() { HttpParameterDescriptor param = CreateParameterDescriptor(); ModelBinderAttribute attribute = new ModelBinderAttribute(typeof(CustomModelBinder)); ModelBinderParameterBinding binding = (ModelBinderParameterBinding)param.BindWithAttribute(attribute); Assert.NotNull(binding); Assert.IsType <CustomModelBinder>(binding.Binder); }
private HttpParameterBinding DetermineBinding(HttpParameterDescriptor parameter) { HttpConfiguration config = parameter.Configuration; var attr = new ModelBinderAttribute(); // use default settings ModelBinderProvider provider = attr.GetModelBinderProvider(config); IModelBinder binder = provider.GetBinder(config, parameter.ParameterType); // Alternatively, we could put this ValueProviderFactory in the global config. var valueProviderFactories = new List<ValueProviderFactory>(attr.GetValueProviderFactories(config)) { new BodyValueProviderFactory() }; return new ModelBinderParameterBinding(parameter, binder, valueProviderFactories); }
public void GetBinderWithBadConstructorThrows() { // Arrange ModelBinderAttribute attr = new ModelBinderAttribute(typeof(BadConverter)); // Act & Assert Assert.Throws<InvalidOperationException>( delegate { attr.GetBinder(); }, "An error occurred when trying to create the IModelBinder 'System.Web.Mvc.Test.ModelBinderAttributeTest+BadConverter'. Make sure that the binder has a public parameterless constructor."); }
public void GetBinderWithBadConstructorThrows() { // Arrange ModelBinderAttribute attr = new ModelBinderAttribute(typeof(BadConverter)); // Act & Assert Assert.Throws <InvalidOperationException>( delegate { attr.GetBinder(); }, "An error occurred when trying to create the IModelBinder 'System.Web.Mvc.Test.ModelBinderAttributeTest+BadConverter'. Make sure that the binder has a public parameterless constructor."); }
public void NoBinderType_NoBindingSource() { // Arrange var attribute = new ModelBinderAttribute(); // Act var source = attribute.BindingSource; // Assert Assert.Null(source); }
public void BinderTypePassedToConstructor_DefaultCustomBindingSource() { // Arrange var attribute = new ModelBinderAttribute(typeof(ByteArrayModelBinder)); // Act var source = attribute.BindingSource; // Assert Assert.Same(BindingSource.Custom, source); }
public void GetBinder() { // Arrange ModelBinderAttribute attr = new ModelBinderAttribute(typeof(GoodConverter)); // Act IModelBinder binder = attr.GetBinder(); // Assert Assert.IsType <GoodConverter>(binder); }
public void GetBinder() { // Arrange ModelBinderAttribute attr = new ModelBinderAttribute(typeof(GoodConverter)); // Act IModelBinder binder = attr.GetBinder(); // Assert Assert.IsType<GoodConverter>(binder); }
public void GetBinder() { // Arrange Type binderType = typeof(GoodConverter); ModelBinderAttribute attr = new ModelBinderAttribute(binderType); // Act IModelBinder binder = attr.GetBinder(); // Assert Assert.IsInstanceOfType(binder, binderType); }
public void BinderType_DefaultCustomBindingSource() { // Arrange var attribute = new ModelBinderAttribute(); attribute.BinderType = typeof(ByteArrayModelBinder); // Act var source = attribute.BindingSource; // Assert Assert.Equal(BindingSource.Custom, source); }
/// <summary> /// Gets the binding for this HTTP header parameter /// </summary> /// <param name="parameter"></param> /// <returns></returns> public override HttpParameterBinding GetBinding(HttpParameterDescriptor parameter) { if (parameter == null) { throw new ArgumentNullException("parameter"); } // combine the model binder for this configuration with our value provider var httpConfig = parameter.Configuration; var binder = new ModelBinderAttribute().GetModelBinder(httpConfig, parameter.ParameterType); return(new ModelBinderParameterBinding(parameter, binder, new ValueProviderFactory[] { new HeaderValueProviderFactory() })); }
public void BinderType_SettingBindingSource_OverridesDefaultCustomBindingSource() { // Arrange var attribute = new ModelBinderAttribute(); attribute.BindingSource = BindingSource.Query; attribute.BinderType = typeof(ByteArrayModelBinder); // Act var source = attribute.BindingSource; // Assert Assert.Equal(BindingSource.Query, source); }
public void GetBinderWithBadConstructorThrows() { // Arrange Type binderType = typeof(BadConverter); ModelBinderAttribute attr = new ModelBinderAttribute(binderType); // Act & Assert ExceptionHelper.ExpectInvalidOperationException( delegate { attr.GetBinder(); }, "There was an error creating the IModelBinder 'System.Web.Mvc.Test.ModelBinderAttributeTest+BadConverter'." + " Check that it has a public parameterless constructor."); }
private void BinderType_From_DependencyResolver_Impl(IDependencyResolver resolver) { // To test dependency resolver, the registered type and actual type should be different. HttpConfiguration config = new HttpConfiguration(); var mockDependencyResolver = new Mock <IDependencyResolver>(); mockDependencyResolver.Setup(r => r.GetService(typeof(CustomModelBinderProvider))).Returns(new SecondCustomModelBinderProvider()); config.DependencyResolver = mockDependencyResolver.Object; ModelBinderAttribute attr = new ModelBinderAttribute(typeof(CustomModelBinderProvider)); ModelBinderProvider provider = attr.GetModelBinderProvider(config); Assert.IsType <SecondCustomModelBinderProvider>(provider); }
public void InvalidBinderType_Throws() { // Arrange var attribute = new ModelBinderAttribute(); var expected = "The type 'System.String' must implement either " + "'Microsoft.AspNet.Mvc.ModelBinding.IModelBinder' or " + "'Microsoft.AspNet.Mvc.ModelBinding.IModelBinderProvider' to be used as a model binder."; // Act var ex = Assert.Throws <InvalidOperationException>(() => { attribute.BinderType = typeof(string); }); // Assert Assert.Equal(expected, ex.Message); }
public void InvalidBinderType_Throws() { // Arrange var attribute = new ModelBinderAttribute(); var expected = "The type 'System.String' must implement either " + "'Microsoft.AspNet.Mvc.ModelBinding.IModelBinder' or " + "'Microsoft.AspNet.Mvc.ModelBinding.IModelBinderProvider' to be used as a model binder."; // Act var ex = Assert.Throws<InvalidOperationException>(() => { attribute.BinderType = typeof(string); }); // Assert Assert.Equal(expected, ex.Message); }
public void BinderType_From_DependencyResolver_ReleasedWhenConfigIsDisposed() { // Arrange HttpConfiguration config = new HttpConfiguration(); var mockDependencyResolver = new Mock <IDependencyResolver>(); SecondCustomModelBinderProvider provider = new SecondCustomModelBinderProvider(); mockDependencyResolver.Setup(r => r.GetService(typeof(CustomModelBinderProvider))).Returns(provider); config.DependencyResolver = mockDependencyResolver.Object; ModelBinderAttribute attr = new ModelBinderAttribute(typeof(CustomModelBinderProvider)); attr.GetModelBinderProvider(config); // Act config.Dispose(); // Assert mockDependencyResolver.Verify(dr => dr.Dispose(), Times.Once()); }
public FromServicesAttribute() { this.modelBinder = new ModelBinderAttribute(typeof(FromServicesModelBinder)); }