Asp.net Core2.0 webapi 项目中 如何在自定义中间件中获取Action的返回值?
为什么HttpContext.Response.Body。不可读取。
亲测 stackoverflow 上的这个解决方法有效:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.Use(async (context, next) =>
{
using (var ms = new MemoryStream())
{
var orgBodyStream = context.Response.Body;
context.Response.Body = ms;
await next();
using (var sr = new StreamReader(ms))
{
ms.Seek(0, SeekOrigin.Begin);
Console.WriteLine("Read Response.Body:\n" + sr.ReadToEnd());
ms.Seek(0, SeekOrigin.Begin);
await ms.CopyToAsync(orgBodyStream);
context.Response.Body = orgBodyStream;
}
}
});
app.UseMvc();
}