简体   繁体   English

在 owin 中间件中读取响应体

[英]Reading the response body in owin middleware

I'm trying to get a copy of a response after my MVC controller action has executed but from other questions on here I can't get this code working (even though this appeared to be a well answered problem) ...我正在尝试在我的 MVC 控制器操作执行后获取响应的副本,但从这里的其他问题来看,我无法使此代码正常工作(尽管这似乎是一个很好回答的问题)...

public class Startup
{
   public void Configuration(IAppBuilder app)
   {
     app.Use(async (context, next) =>
     {
        var resultStream = context.Response.Body;
        context.Response.Body = new MemoryStream();

        // allow the response to be written in future request lifecycle events
        await next.Invoke();

        // fetch the repsonse
        context.Response.Body.Seek(0, SeekOrigin.Begin);
        var headers = context.Response.Headers;
        var body = new StreamReader(context.Response.Body).ReadToEnd();

        // ... other code omitted for question clarity

        // write the response to the client
        context.Response.Body.Seek(0, SeekOrigin.Begin);
        await context.Response.Body.CopyToAsync(resultStream);
        context.Response.Body = resultStream;
      });
   }

   // ... other code omitted for question clarity
}

When I get to the second seek the variable "body" is empty.当我进行第二次搜索时,变量“body”为空。

Any ideas why this may be the case when the result after this is a page with content ?当此之后的结果是包含内容的页面时,为什么会出现这种情况?

So it turns out the problem here is to do with the fact that Owin and asp.net are not intertwined in the full lifecycle together as I had thought.所以事实证明,这里的问题是因为 Owin 和 asp.net 在整个生命周期中没有像我想象的那样交织在一起。

In short, the request lifecycle looks something like ...简而言之,请求生命周期看起来像......

  1. Do Owin stuff (all owin middlewares)做 Owin 的东西(所有 owin 中间件)
  2. Do MVC stuff做MVC的东西
  3. Do server stuff做服务器的东西

... what I need is ... ……我需要的是……

  1. Do owin stuff做自己的东西
  2. Do MVC stuff做MVC的东西
  3. Do more Owin stuff做更多 Owin 的事情
  4. Do server stuff做服务器的东西

... of course i'm hugely over simplifying the process here but i guess the short way to explain it is when you do ... ...当然,我在这里大大简化了过程,但我想解释它的简短方法是当你这样做时......

app.Use((context, next) => { ... }).UsestageMarker(?);

... there is no stage marker for "response processing complete". ...“响应处理完成”没有阶段标记。

Interestingly aspnet core gives us much more control as the tight integration with all the pieces throughout the request lifecycle are less dependent on predefined stages and more about you the user defining your own process for handling requests and constructing the response.有趣的是,aspnet 核心为我们提供了更多的控制权,因为在整个请求生命周期中与所有部分的紧密集成较少依赖于预定义的阶段,而更多地依赖于用户定义自己的处理请求和构建响应的过程。

In short ... in aspnet core I can do what i'm trying to do but this does not appear to be possible in owin with .net 4.6简而言之......在aspnet核心中,我可以做我想做的事情,但这在.net 4.6中似乎不可能

I did however a few references to using filters and handling "OnActionExectuted" which if you look you have a completely different Request and Response object than those given to you by owin middleware's pipeline (thus adding more evidence that these things are not in fact one single process but simply two that happen in an order).然而,我做了一些关于使用过滤器和处理“OnActionExecuted”的参考,如果你看起来你有一个与 owin 中间件管道提供给你的完全不同的请求和响应对象(因此增加了更多的证据表明这些东西实际上不是一个单一的过程,但只是按顺序发生的两个)。

I have since spent the time looking at migrating my application to aspnet core ... which is proving to be more of a headache than i had anticipated, but i'm ever hopeful of the final result being cleaner and faster.从那以后,我一直在研究将我的应用程序迁移到 aspnet 核心……事实证明,这比我预期的更令人头疼,但我一直希望最终结果更清晰、更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM