中间件中的路由控制器和动作 [英] Route controller and action in middleware

查看:110
本文介绍了中间件中的路由控制器和动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检索控制器和动作,我已经尝试通过使用

I am trying to retrieve the controller and action, I have tried to this point by using

var routeData = context.GetRouteData();

在中间件的Invoke方法内部,但每次都会产生null.

inside the middleware's Invoke method, but it yields null every time.

是否可以在中间件中检索路线数据?

Is it possible at all to retrieve route data in middleware?

我要实现的目的是检查所请求的操作是否具有[RequireToken]属性,如果是,它将检查传入的标头以查找特定的令牌.

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.

推荐答案

操作/控制器上下文非常特定于中间件管道的MVC部分.无法在MVC管道本身之外检索路由数据.

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.

为了获得所需的行为,您应该查看实现ActionFilter的替代方法:

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

这篇关于中间件中的路由控制器和动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆