.net core 3.0 在过滤器读取request.body 里的请求,controller[FromBody]读取不到参数,解决办法
1,注入IHttpContextAccessor httpContex
2,var req = _httpContext.HttpContext.Request;
// 这句很重要,开启读取 否者下面设置读取为0会失败
req.EnableBuffering();
using (var reader = new StreamReader(_httpContext.HttpContext.Request.Body, Encoding.UTF8, true, 1024, true))
{
var a = await reader.ReadToEndAsync();
id = JsonConvert.DeserializeObject<View>(a).Id;
}
// 这里读取过body Position是读取过几次 而此操作优于控制器先行 控制器只会读取Position为零次的
req.Body.Position = 0;