I am trying to retrieve the controller and action, I have tried to this point by using
var routeData = context.GetRouteData();
inside the middleware's Invoke method, but it yields null every time.
Is it possible at all to retrieve route data in middleware?
What I am trying to achieve is to check whether the requested action has a [RequireToken] attribute, and if so, it will check the incoming headers for a specific token.
The action/controller context is very specific to the MVC portion of the middleware pipeline. It is not possible to retrieve the Route Data outside of the MVC pipeline itself.
In order to achieve the desired behaviour, you should look at implementing an ActionFilter instead: https://docs.asp.net/en/latest/mvc/controllers/filters.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With