Reputation: 2564
I'm trying to write a custom model binder that can bind a property decorated with an attribute to a differently-named request property e.g.
{
"app": "acme"
}
[Alias("app")]
public string ApplicationName { get; set; }
... should result in ApplicationName
being populated with the value 'acme'. I'm getting stuck writing the custom model binder for this:
public BindToAliasModelBinder : IModelBinder {
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) {
...
}
}
public class BindFromAliasModelBinderProvider : ModelBinderProvider {
public override IModelBinder GetBinder(HttpConfiguration configuration, Type modelType) {
return new BindFromAliasModelBinder();
}
}
I've registered the provider globally and the binder is being hit as expected. I'm at a loss for what to do next - how do I iterate through the request values and conditionally bind based on the presence of the attribute?
Upvotes: 2
Views: 637
Reputation: 6793
If all you want to do is aliasing, you can use JsonPropertyAttribute
, something like [JsonProperty(PropertyName = "app")]
on the property.
Upvotes: 1