.net core acquisition parameters passed on the server side api

Using the following code ActionFilterAttribute OnActionExecutionAsync user parameters read from the stream

// read the delivery of the measured parameters from the file stream
            using (var ms = new MemoryStream())
            {
                context.HttpContext.Request.Body.Seek (0, 0); // pointer to the read start position
                context.HttpContext.Request.Body.CopyTo(ms);
                var b = ms.ToArray();
                var postParamsString = Encoding.UTF8.GetString(b);
            }

 Although previously knew it was read from the stream, but the .net core is quite difficult to find nearly two hours to find a way to read parameters from the stream, the key phrase: context.HttpContext.Request.Body. Seek (0, 0); otherwise the contents read is empty

The complete code

public class SignValidateAttribute : ActionFilterAttribute
    {
        #region
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        public async override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            // read the delivery of the measured parameters from the file stream
            using (var ms = new MemoryStream())
            {
                context.HttpContext.Request.Body.Seek(0, 0);
                context.HttpContext.Request.Body.CopyTo(ms);
                var b = ms.ToArray();
                var postParamsString = Encoding.UTF8.GetString(b);
                await next();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="next"></param>
        /// <returns></returns>
        public override Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
        {
            //string dataJson = GetContextJson(context.);
            return base.OnResultExecutionAsync(context, next);
        }
        #endregion

    }

  

Guess you like

Origin www.cnblogs.com/bamboo-zhang/p/11727705.html