, IModelBinder , , , () InjectedObject. , "your_key":
public class InjectedObjectModelBinder : IModelBinder
{
public bool BindModel(System.Web.Http.Controllers.HttpActionContext actionContext, ModelBindingContext bindingContext)
{
if (bindingContext.ModelType != typeof(InjectedObject))
{
return false;
}
IEnumerable<string> values;
string keyValue = "";
if (actionContext.Request.Headers.TryGetValues("your_key", out values))
{
keyValue = values.First();
}
bindingContext.Model = new InjectedObject() { Id = 789, Name = keyValue };
return true;
}
}
InjectedObject. :
-, :
public void Post([ModelBinder(typeof(InjectedObjectModelBinder))]InjectedObject a, ConstructedObject b)
, , . -, , InjectedObject ModelBinder:
[ModelBinder(typeof(InjectedObjectModelBinder))]
public class InjectedObject
{....
, HttpConfiguration Register WebApiConfig:
var provider = new SimpleModelBinderProvider(typeof(InjectedObject), new InjectedObjectModelBinder());
config.Services.Insert(typeof(ModelBinderProvider), 0, provider);
ConstructedObject InjectedObject :
[ModelBinder(typeof(InjectedObjectModelBinder))]
public class InjectedObject
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ConstructedObject
{
public int A { get; set; }
public int B { get; set; }
public string C { get; set; }
}
, :
public void Post(InjectedObject a, ConstructedObject b)
{
}
, , Fiddler, :
POST http://localhost:64577/api/values HTTP/1.1
Host: localhost:64577
Accept: */*
Content-Type: application/json
Connection: keep-alive
Content-Length: 51
your_key: This came from the header
{"a":1,"b":2,"c":"This was 'normal' model binding"}
, /:

Web API .