public static async Task PrepareRequestBody(HttpRequest request)
        {
            if (!request.Body.CanSeek)
            {
                request.EnableBuffering();
                await StreamHelperExtensions.DrainAsync(request.Body, CancellationToken.None);
            }

            request.Body.Seek(0L, SeekOrigin.Begin);
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public override async Task <InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }
            var request = context.HttpContext.Request;

            if (!request.Body.CanSeek)
            {
                HttpRequestRewindExtensions.EnableBuffering(request);
                await StreamHelperExtensions.DrainAsync(request.Body, CancellationToken.None);

                request.Body.Seek(0L, SeekOrigin.Begin);
            }

            using (TextReader textReader = context.ReaderFactory(request.Body, encoding))
            {
                var modelType = context.ModelType;
                if (modelType.IsArray)
                {
                    var itemType = modelType.GetElementType();
                    var arr      = new ArrayList();
                    while (true)
                    {
                        var line = textReader.ReadLine();
                        if (string.IsNullOrEmpty(line))
                        {
                            break;
                        }
                        var obj = GetModel(line, itemType);
                        arr.Add(obj);
                    }
                    var modelList = arr.ToArray(itemType);
                    return(await InputFormatterResult.SuccessAsync(modelList));
                }
                else if (typeof(IEnumerable).IsAssignableFrom(modelType))
                {
                    var   itemType  = modelType.GenericTypeArguments[0];
                    var   listType  = typeof(List <>).MakeGenericType(itemType);
                    IList modelList = (IList)Activator.CreateInstance(listType);
                    while (true)
                    {
                        var line = textReader.ReadLine();
                        if (string.IsNullOrEmpty(line))
                        {
                            break;
                        }
                        var obj = GetModel(line, itemType);
                        modelList.Add(obj);
                    }
                    return(await InputFormatterResult.SuccessAsync(modelList));
                }
                else
                {
                    var obj = GetModel(textReader.ReadLine(), modelType);
                    return(await InputFormatterResult.SuccessAsync(obj));
                }
            }
        }