public override Task <InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding) { if (context == null) { throw new ArgumentNullException(nameof(context)); } encoding = encoding ?? SelectCharacterEncoding(context); if (encoding == null) { throw new ArgumentNullException(nameof(encoding)); } var request = context.HttpContext.Request; try { object inputModel = null; var formCollection = request.Form.ToDictionary(f => f.Key, f => f.Value.ToString()); if (formCollection.Count > 0 && FormUrlEncodedJson.TryParse(formCollection, out var jObject)) { inputModel = jObject.ToObject(context.ModelType); } return(Task.FromResult(inputModel != null ? InputFormatterResult.Success(inputModel) : InputFormatterResult.Failure())); } catch (Exception) { return(Task.FromResult(InputFormatterResult.Failure())); } }
public override async Task <InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding) { if (context == null) { throw new ArgumentNullException(nameof(context)); } encoding = encoding ?? SelectCharacterEncoding(context); if (encoding == null) { throw new ArgumentNullException(nameof(encoding)); } var request = context.HttpContext.Request; try { var type = context.ModelType; var commandType = type; if ((type.IsAbstract || type.IsInterface) && typeof(ICommand).IsAssignableFrom(type)) { commandType = GetCommandType(request.GetUri().Segments.Last()); } var mediaType = request.ContentType.Split(';').FirstOrDefault(); object command = null; if (mediaType == ApplicationFormUrlEncodedFormMediaType) { if (FormUrlEncodedJson.TryParse(request.Form.ToDictionary(f => f.Key, f => f.Value.ToString()), out var jObject)) { command = jObject.ToObject(commandType); } } else { using (var streamReader = context.ReaderFactory(request.Body, encoding)) { var part = await streamReader.ReadToEndAsync(); command = part.ToJsonObject(commandType); } } return(command != null?InputFormatterResult.Success(command) : InputFormatterResult.Failure()); } catch (Exception) { return(InputFormatterResult.Failure()); } }