从OWIN中间件更改响应对象 [英] Changing the response object from OWIN Middleware

查看:137
本文介绍了从OWIN中间件更改响应对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我OWIN中间件是这样的。 (Framework是的ASP.NET Web API)。

 公共类MyMiddleware:OwinMiddleware
{
    公共MyMiddleware(OwinMiddleware下一个):基地(下){}    公共覆盖异步任务调用(OwinRequest要求,OwinResponse响应)
    {
        VAR头= request.GetHeader(X-无论标头);        等待Next.Invoke(请求,响应);        response.SetHeader(X-MyResponse标头,一些价值);
        response.Status code = 403;    }
}

问题:


  1. 这是值得推荐的做法从 OwinMiddleware 来获得?我看到,在卡塔纳源,一些中间件类从 OwinMiddleware 导出,有些则没有。


  2. 我可以看到请求头还行。在我的中间件设置响应头或状态code后 Next.Invoke 对响应返回给客户端没有影响。但是,如果我设定的响应头或状态 Next.Invoke之前通话,与头的反应和我集返回给客户端的状态。什么是这些设置的正确方法吗?



解决方案

  1. 是,建议从OwinMiddleware派生。一些中间件类不从OwinMiddleware派生的原因是,要么是因为该类最近已推出他们没有切换呢。或以避免大会采取对Microsoft.Owin装配出于某种原因的依赖关系。


  2. 在呼吁下调用不工作后的反应的可能的原因设置的东西是因为任何人都开始写响应体流的响应HTTP头,即会发送。因此,中间件组件后的任何更改状态code或HTTP头开始写入响应身体不会有任何影响。


什么你可以尝试做是使用OnSendingHeaders回调OWIN提供。这里是你如何使用它:

公众覆盖异步任务调用(IOwinContext上下文)
{
   VAR响应= context.Response;
   VAR请求= context.Request;   response.OnSendingHeaders(州=>
   {
       VAR RESP =(OwinResponse)状态;
       resp.Headers.Add(X-MyResponse标头,一些价值);
       resp.Status code = 403;
       resp.ReasonPhrase =禁止;
    },响应);  VAR头= request.Headers [X-无论标头];  等待Next.Invoke(背景);
}

感谢biscuit314更新我的答案。

My OWIN middleware is like this. (Framework is ASP.NET Web API).

public class MyMiddleware : OwinMiddleware
{
    public MyMiddleware(OwinMiddleware next) : base(next) { }

    public override async Task Invoke(OwinRequest request, OwinResponse response)
    {
        var header = request.GetHeader("X-Whatever-Header");

        await Next.Invoke(request, response);

        response.SetHeader("X-MyResponse-Header", "Some Value");
        response.StatusCode = 403;

    }
}

Questions:

  1. Is it the recommended practice to derive from OwinMiddleware? I see that in Katana source, some of the middleware classes derive from OwinMiddleware and some do not.

  2. I can see the request headers okay. Setting response header or status code after Next.Invoke in my middleware has no effect on the response returned to the client. But if I set the response header or status before the Next.Invoke call, the response with headers and the status that I set is returned to the client. What is the right way of setting these?

解决方案

  1. Yes, deriving from OwinMiddleware is recommended. The reason some middleware classes don't derive from OwinMiddleware is that either they haven't switched over yet because the class was introduced recently. Or to avoid having the assembly take a dependency on the Microsoft.Owin assembly for some reason.

  2. The probable reason setting stuff on the response after calling Invoke on Next doesn't work is that the response HTTP header gets sent as soon as anyone starts writing to the response body stream. So any changes to status code or HTTP headers after a middleware component starts writing to the response body won't have any effect.

What you can try doing is to use the OnSendingHeaders callback that OWIN provides. Here's how you can use it:

public override async Task Invoke(IOwinContext context)
{
   var response = context.Response;
   var request =  context.Request;

   response.OnSendingHeaders(state =>
   {
       var resp = (OwinResponse)state;
       resp.Headers.Add("X-MyResponse-Header", "Some Value");
       resp.StatusCode = 403;
       resp.ReasonPhrase = "Forbidden";
    }, response);

  var header = request.Headers["X-Whatever-Header"];

  await Next.Invoke(context);
}

Credit to biscuit314 for updating my answer.

这篇关于从OWIN中间件更改响应对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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