简体   繁体   中英

asp.net core - How to pass variable from controller to filter

I have a controller. On the controller's post method, there is a variable (an object variable) I want to pass to my action filter: the method:

public void OnActionExecuted(ActionExecutedContext context)...

Any suggestions on how achieve this?

Use HttpContext.Items . It's a key / value collection that exists for the duration of a single request.

In your controller, add something:

HttpContext.Items["Something"] = "something I need later";

Then in the OnActionExecuted method pull it out:

var something = context.HttpContext.Items["Something"] as string;

Everything you pull out will be of type object so make sure you cast it to what it was.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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