首页 新闻 赞助 找找看

.net core 如何在 中间件 中获取 当前执行的 action 的 自定义特性

0
悬赏园豆:50 [已解决问题] 解决于 2019-03-12 17:04

我想实现一个 全局的统一日志管理
使用中间件来统一管理
有一些 action 不需要记录日志 可以给 这些 action 加上特殊的 Attribute

可是我在中间件中要获取 当前执行的 action 的特性 怎么做到

RouteData 得到数据了
最后得不到Action

Skolley的主页 Skolley | 初学一级 | 园豆:136
提问于:2019-03-11 20:51

管道不是 先进后出的么 我加在了第一
个 而且是在 调用完 next()之后的处理 所以action肯定是执行过了

Skolley 5年前
< >
分享
最佳答案
3

endpoint routing 闪亮登场,以下代码来自 https://github.com/aspnet/AspNetCore/issues/7795

// This helper method comes in 3.0. For now you'll need a copy in your app
public static Endpoint GetEndpoint(HttpContext context)
{
    if (context == null)
    {
       throw new ArgumentNullException(nameof(context));
    }

    return context.Features.Get<IEndpointFeature>()?.Endpoint;
}
var endpoint = GetEndpoint(context);

// Endpoint will be null if the URL didn't match an action, e.g. a 404 response
if (endpoint != null)
{
    var yourAttribute = endpoint.Metadata.GetMetadata<YourAttributeOnTheMvcAction>();
}
收获园豆:50
dudu | 高人七级 |园豆:31075 | 2019-03-11 21:54
其他回答(1)
0

不可能得到,中间件是拦截请求和响应,你这还没执行到Action呢!

Jeffcky | 园豆:2789 (老鸟四级) | 2019-03-11 21:11

我这个是在 执行过 next() 之后拿的 action已经执行过了
RouteData 里也有 controller 和 action的值了

支持(0) 反对(0) Skolley | 园豆:136 (初学一级) | 2019-03-11 21:14

@Skolley: 哦,这样。

支持(0) 反对(0) Jeffcky | 园豆:2789 (老鸟四级) | 2019-03-11 21:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册