Click or drag to resize

RegistrationExtensionsRegisterModelBinders Method

Register types that implement IModelBinder in the provided assemblies and have a ModelBinderTypeAttribute.

Namespace:  Autofac.Integration.Mvc
Assembly:  Autofac.Integration.Mvc (in Autofac.Integration.Mvc.dll) Version: 6.0.0+106fe194fc419c09f32d39119967f68798cc6895
Syntax
public static IRegistrationBuilder<Object, ScanningActivatorData, DynamicRegistrationStyle> RegisterModelBinders(
	this ContainerBuilder builder,
	params Assembly[] modelBinderAssemblies
)

Parameters

builder
Type: AutofacContainerBuilder
The container builder.
modelBinderAssemblies
Type: System.ReflectionAssembly
Assemblies to scan for model binders.

Return Value

Type: IRegistrationBuilderObject, ScanningActivatorData, DynamicRegistrationStyle
A registration builder.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type ContainerBuilder. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Exceptions
ExceptionCondition
ArgumentNullException Thrown if builder or modelBinderAssemblies is .
Remarks

The declarative mechanism of registering model binders with Autofac is through use of this method and the ModelBinderTypeAttribute. If you would like more imperative control over registration for your model binders, see the AsModelBinderForTypesTLimit, TActivatorData, TRegistrationStyle(IRegistrationBuilderTLimit, TActivatorData, TRegistrationStyle, Type) method.

The two mechanisms are mutually exclusive. If you register a model binder using AsModelBinderForTypesTLimit, TActivatorData, TRegistrationStyle(IRegistrationBuilderTLimit, TActivatorData, TRegistrationStyle, Type) and register the same model binder with this method, the results are not automatically merged together - standard dependency registration/resolution rules will be used to manage the conflict.

This method only registers types that implement IModelBinder and are marked with the ModelBinderTypeAttribute. The model binder must have the attribute because the AutofacModelBinderProvider uses the associated metadata - from the attribute(s) - to resolve the binder based on model type. If there aren't any attributes, there won't be any metadata, so the model binder will be technically registered but will never actually be resolved.

If your model is not marked with the attribute, or if you don't want to use attributes, use the AsModelBinderForTypesTLimit, TActivatorData, TRegistrationStyle(IRegistrationBuilderTLimit, TActivatorData, TRegistrationStyle, Type) extension instead.

See Also