Route controller and action in middleware

孤者浪人 提交于 2019-12-07 04:15:41

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/39335824/route-controller-and-action-in-middleware

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!