Пример #1
0
        public override async Task <InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
        {
            var request     = context.HttpContext.Request;
            var contentType = context.HttpContext.Request.ContentType;

            if (string.IsNullOrEmpty(contentType) ||
                ContentMediaTypes.IsContentType(contentType, ContentMediaTypes.Text.Plain))
            {
                using (var reader = new StreamReader(request.Body))
                {
                    var content = await reader.ReadToEndAsync().ConfigureAwait(false);

                    return(await InputFormatterResult.SuccessAsync(content).ConfigureAwait(false));
                }
            }

            if (!ContentMediaTypes.IsContentType(contentType, ContentMediaTypes.Application.OctetStream))
            {
                return(await InputFormatterResult.FailureAsync().ConfigureAwait(false));
            }

            await using (var ms = new MemoryStream(2048))
            {
                await request.Body.CopyToAsync(ms).ConfigureAwait(false);

                var content = ms.ToArray();
                return(await InputFormatterResult.SuccessAsync(content).ConfigureAwait(false));
            }
        }
Пример #2
0
        public override bool CanRead(InputFormatterContext context)
        {
            Ensure.IsNotNull(context, nameof(context));

            var contentType = context.HttpContext.Request.ContentType;

            return(string.IsNullOrEmpty(contentType) ||
                   ContentMediaTypes.IsContentType(contentType, ContentMediaTypes.Text.Plain) ||
                   ContentMediaTypes.IsContentType(contentType, ContentMediaTypes.Application.OctetStream));
        }