asp.net core 把上传的json转成string

    public class BodyReaderAttribute : ActionFilterAttribute
    {
        private string parameterName;
        public BodyReaderAttribute(string parameterName = "content")
        {
            this.parameterName = parameterName;
        }

        public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var syncIOFeature = context.HttpContext.Features.Get<IHttpBodyControlFeature>();
            if (syncIOFeature != null)
            {
                syncIOFeature.AllowSynchronousIO = true;
            }

            var request = context.HttpContext.Request;
            request.EnableBuffering();
            var reader = new StreamReader(request.Body);
            string content = reader.ReadToEnd();
            request.Body.Seek(0, SeekOrigin.Begin);
            context.ActionArguments.Add(this.parameterName, content);

            await base.OnActionExecutionAsync(context, next);
        }
    }
posted @ 2020-08-25 13:10  whyfate  阅读(563)  评论(0编辑  收藏  举报