Register a ModelBinderProvider with Autofac in ASP.NET MVC

2

For an ASP.NET MVC5 application I have created a ModelBinderProvider for a certain type of data.

If I add the new ModelBinderProvider to the BinderProviders collection in the Global.asax it works perfectly.

ModelBinderProviders.BinderProviders.Add(new CustomModelBinderProvider());

In the application I am using Autofac as IoC which allows me to register for example my custom ModelBinders:

Autofac.Integration.Mvc.RegistrationExtensions.AsModelBinderForTypes(
    builder.RegisterType<CustomModelBinder>(), typeof(CustomModel));

But I have not found a way to register the ModelProvider with Autofac.

Is there a way to do it?

    
asked by Asier Villanueva 13.05.2016 в 09:35
source

2 answers

1

Solved:

builder.Register(c => new CustomModelBinderProvider())
   .As<IModelBinderProvider>()
    
answered by 13.05.2016 / 23:58
source
0

Have you applied the Autofac.Integration.Mvc.ModelBinderTypeAttribute? attribute to that class?

Then to register you should do

builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
builder.RegisterModelBinderProvider();

The source of this answer you can take a look at link

    
answered by 13.05.2016 в 12:11