Skip to content

Instantly share code, notes, and snippets.

@AndyButland
Last active May 30, 2023 15:43
Show Gist options
  • Save AndyButland/fd5c7ec8b07d44c7b09d4b65abd65858 to your computer and use it in GitHub Desktop.
Save AndyButland/fd5c7ec8b07d44c7b09d4b65abd65858 to your computer and use it in GitHub Desktop.
public class FormEntryDtoModelBinder : IModelBinder
{
public async Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
using (var reader = new StreamReader(bindingContext.HttpContext.Request.Body))
{
var body = await reader.ReadToEndAsync().ConfigureAwait(continueOnCapturedContext: false);
FormEntryDto? value = JsonConvert.DeserializeObject<FormEntryDto>(body);
if (value != null)
{
bindingContext.Result = ModelBindingResult.Success(value);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment