在Startup.cs中定義Middleware,設置緩存Http請求的Body數據。代碼如下。自定義Middleware請放到Configure方法的最前面。
app.Use(next => new RequestDelegate(async context => {context.Request.EnableBuffering();await next(context);}));
GET請求
HttpContext.Request.Query
//或者
HttpContext.Request.QueryString
POST請求
form
HttpContext.Request.Form
body
HttpContext.Request.Body//過濾器中使用
public override async Task OnExceptionAsync(ExceptionContext context){var httpContext = context.HttpContext;var request = httpContext.Request;request.Body.Position = 0;StreamReader sr = new StreamReader(request.Body);string body = await sr.ReadToEndAsync();request.Body.Position = 0;}}
file
HttpContext.Request.Form.Files