Jonas
Jonas

Reputation: 5149

WebAPI model property binding through model property attributes

How to implement property binding using property attributes like in MVC described in this article. Tried to override standard binders with no success. Any ideas how to reuse WebAPI standard binders functionality while use custom property binding?

Upvotes: 0

Views: 569

Answers (1)

Jonas
Jonas

Reputation: 5149

Finally found out that TypeConverter's are for this purpose. There is one important thing to know. If complex type is standard type, let say StringDictionary. Then you must override StringDictionary and set TypeConverter attribute for your new class like this:

[TypeConverter(typeof(MyTypeConverter))]
public class MyStringDictionary : StringDictionary { }

use MyStringDictionary in your model. This way custom conversion is not working:

public class MyModel
{
    [TypeConverter(typeof(MyTypeConverter))]
    public StringDictionary MyProp { get; set; }
}

Upvotes: 1

Related Questions