Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Custom view model binders

Alexanderius edited this page Jun 30, 2019 · 3 revisions

Custom view model binders

To use custom model view binder you should register it using HttpModelHandler.RegisterModelBinder, it will be added to binders pipeline.

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ...

        HttpModelHandler.RegisterModelBinder<MyModelBinder>();

        app.UseSimplifyWeb();
    }
}

Binder should be deriver from IModelBinder interface.

public class MyModelBinder : IModelBinder
{
    public void Bind<T>(ModelBinderEventArgs<T> args)
    {
        // deserialization logic
    }
}

If you just want to use your binder without default binders, then you should clear binders list first.

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ...
        HttpModelHandler.ModelBindersTypes.Clear();
        HttpModelHandler.RegisterModelBinder<MyModelBinder>();

        app.UseSimplifyWeb();
    }
}

One example of custom model binder is JsonModelBinder.

<< Previous page Next page >>

Clone this wiki locally