public class UsernameBinder: System.Web.Http.ModelBinding.IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
var key = bindingContext.ModelName;
var val = bindingContext.ValueProvider.GetValue(key);
if (val != null)
{
//?????????????????????????????????????????
//Get username property of user
//Set username property = User.Identity.Name
}
return false;
}
}
I want to create a model binder that binds username of user automatically. But I cauld not get property.
And I will use it like this:
public SendMessage CreateMessage([ModelBinder(typeof(UsernameBinder))]Message message)
{
}
How can I get property of a model from Web API?