我的OwinMiddleware
Invoke
方法看起来像这样:
public override async Task Invoke(IOwinContext context)
{
...
//The next line launches the execution of the Get method of a controller
await Next.Invoke(context);
//Now context.Response should contain "myvalue" right?
...
}
Invoke
方法调用位于控制器内的Get
方法,看起来像这样:
[HttpGet]
public IHttpActionResult Get(some params...)
{
...
return "myvalue";
...
}
执行Get
方法后,程序返回到我的中间件的Invoke
方法。我认为Get
方法的响应,即myvalue
,应该包含在context.Response
中,但我不知道究竟在哪里,因为它充满了的东西。
答案 0 :(得分:0)
Actualy响应是一个流,您需要这样做以获得orignal形式的响应
try{
var stream = context.Response.Body;
var buffer = new MemoryStream();
context.Response.Body = buffer;
await _next.Invoke(environment);
buffer.Seek(0, SeekOrigin.Begin);
var reader = new StreamReader(buffer);
// Here you will get you response body like this
string responseBody = reader.ReadToEndAsync().Result;
// Then you again need to set the position to 0 for other layers
context.Response.Body.Position = 0;
buffer.Seek(0, SeekOrigin.Begin);
await buffer.CopyToAsync(stream);
}
catch(Exception ex)
{
}