Skip to content

Instantly share code, notes, and snippets.

@filipw
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filipw/282a11f406040ff2cf0f to your computer and use it in GitHub Desktop.
Save filipw/282a11f406040ff2cf0f to your computer and use it in GitHub Desktop.
public class FromBodyApplicationModelConvention : IApplicationModelConvention
{
public void Apply(ApplicationModel application)
{
foreach (var controller in application.Controllers)
{
foreach (var action in controller.Actions)
{
foreach (var parameter in action.Parameters)
{
if (parameter.BindingInfo?.BindingSource != null ||
parameter.Attributes.OfType<IBindingSourceMetadata>().Any() ||
ValueProviderResult.CanConvertFromString(parameter.ParameterInfo.ParameterType))
{
// behavior configured or simple type so do nothing
}
else
{
// Complex types are by-default from the body.
parameter.BindingInfo = parameter.BindingInfo ?? new BindingInfo();
parameter.BindingInfo.BindingSource = BindingSource.Body;
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment