为什么我的中间件不工作?

时间:2017-11-02 11:34:53

标签: asp.net asp.net-mvc owin-middleware

我试图添加自己的中间件,它不起作用: - )

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        System.Diagnostics.Debug.WriteLine("OWIN IS WORKING");

        app.Use<CustomMiddleware>();
    }
}

public class CustomMiddleware
{
    public async Task Invoke(HttpContext context)
    {
        string token = context.Request.Headers["Authorization"];
    }
}

WriteLine是这样,它按服务器的每个请求进入配置,但我的CustomMiddleware Invoke aint触发了。

Anye线索?

1 个答案:

答案 0 :(得分:1)

你需要构造函数有下一个delegate

所以你的代码应该改变如下:

public class CustomMiddleware 
{
    private readonly RequestDelegate _next;

    public CustomMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        string token = context.Request.Headers["Authorization"];
        await _next();
    }
}