Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created May 20, 2021 10:55
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 bjoerntx/4410a7ca82e4ff9fbe9f3d36a467b5d4 to your computer and use it in GitHub Desktop.
Save bjoerntx/4410a7ca82e4ff9fbe9f3d36a467b5d4 to your computer and use it in GitHub Desktop.
public sealed class JsonDotNetValueProviderFactory : ValueProviderFactory {
public override IValueProvider GetValueProvider(ControllerContext controllerContext) {
if (controllerContext == null)
throw new ArgumentNullException("controllerContext");
if (!controllerContext.HttpContext.Request.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
return null;
var reader = new StreamReader(controllerContext.HttpContext.Request.InputStream);
var bodyText = reader.ReadToEnd();
return String.IsNullOrEmpty(bodyText) ? null : new DictionaryValueProvider<object>(
JsonConvert.DeserializeObject<ExpandoObject>(bodyText, new ExpandoObjectConverter()), CultureInfo.CurrentCulture);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment