Skip to content

Instantly share code, notes, and snippets.

@AndyButland
Last active May 30, 2023 15:43
Show Gist options
  • Save AndyButland/4e39ad907b0e4bf13c1e1d6907c5b1ea to your computer and use it in GitHub Desktop.
Save AndyButland/4e39ad907b0e4bf13c1e1d6907c5b1ea 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 = JsonSerializer.Deserialize<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