我如何要求Owin/Katana将标头写入输出流? [英] How do I ask Owin/Katana to write headers to the output stream?

查看:65
本文介绍了我如何要求Owin/Katana将标头写入输出流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我写响应时,Katana会跳过发送经过时间响应标头.第一次写流之前,如何为我设置标头?

When I write to the response, Katana skips sending out the Elapsed-Time response header. How can I have it set the headers for me before I write to the stream for the first time?

    public override async Task Invoke(IOwinContext context)
    {
        var stopwatch = new Stopwatch();

        stopwatch.Start();
        await Next.Invoke(context);
        stopwatch.Stop();

        context.Response.Headers.Add("Elapsed-Time", new[] {stopwatch.ElapsedMilliseconds.ToString()});
    }

中间件#2

    public override async Task Invoke(IOwinContext context)
    {
        await context.Response.WriteAsync("test");
    }

推荐答案

经过研究,答案是设置标头需要在OnSendingHeaders内部发生.这样可以确保在写入输出流之前已设置标头.例如:

After some research, the answer is that settings headers needs to happen from within OnSendingHeaders. This ensures that the headers are set before the output stream is written to. For example:

    public override async Task Invoke(IOwinContext context)
    {
        var stopwatch = new Stopwatch();

        context.Response.OnSendingHeaders(x =>
        {
            stopwatch.Stop();
            context.Response.Headers.Add("X-Processing-Time", new[] {stopwatch.ElapsedMilliseconds.ToString()});
        }, null);

        stopwatch.Start();
        await Next.Invoke(context);
        stopwatch.Stop();
    }

这篇关于我如何要求Owin/Katana将标头写入输出流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆