Last active
August 29, 2015 14:20
-
-
Save filipw/282a11f406040ff2cf0f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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