由于.net 5.0 倒帶方式發(fā)生了改變,在asp.net core web api 項目中,框架為.NET5,啟動倒帶方式,為 request.EnableBuffering() 但是在過濾器中使用此方法時出現(xiàn)異常,request.body的長度總是為0,說明在請求到達過濾器時Steam已經(jīng)被讀取了。 錯誤代碼: public class TestFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext context) { base.OnActionExecuting(context); var request = context.HttpContext.Request; //啟動倒帶方式 request.EnableBuffering(); if (request.Method.ToLower().Equals("post")) { request.Body.Position = 0; using var requestReader = new StreamReader(request.Body); var requestContent = requestReader.ReadToEnd(); request.Body.Position = 0; } } public override void OnActionExecuted(ActionExecutedContext context) { base.OnActionExecuted(context); } } FilterModel myModel = JsonConvert.DeserializeObject<FilterModel>(requestContent); |
|
來自: 修行的嘟嘟 > 《軟件開發(fā)》