Pass data to middleware further down the chain

前端 未结 2 2000
有刺的猬
有刺的猬 2021-02-18 20:10

When I register middleware as part of the request pipeline, how do I pass data through the middleware chain. (ultimately accessible in an MVC controller action)

For exam

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-18 20:55

    You can use the HttpContext.Items collection to store data for the lifetime of a request. Its primary use-case is passing data around components (middleware and controllers for example). Adding and reading items is easy:

    Write:

    context.Items["AuthData"] = authData;
    

    Read:

    var authData = (AuthData)context.Items["AuthData"];
    

    See the ASP.NET docs for more info.

提交回复
热议问题