我试过这个: 读取请求正文两次 ,这个:https : //github.com/aspnet/Mvc/issues/4962 但没有用。我这样阅读请求正文:
app.Use(async (context, next) =>
{
var requestBody = await ReadStream(context.Request.Body);
var requestPath = context.Request.Path.ToString();
//Do some thing
await next.Invoke();
var responseStatusCode = context.Response.StatusCode;
//Do some other thing
});
private async Task<string> ReadStream(Stream stream)
{
using (var streamReader = new StreamReader(stream))
{
var result = await streamReader.ReadToEndAsync();
return result;
}
}
Run Code Online (Sandbox Code Playgroud)
在控制器中,我得到“处置对象”或“空流”。