asp.net-web-api - Web API:如何从 HttpContext 读取动作属性和参数
问题描述
在普通课堂上,我需要阅读以下内容HttpContext
:
控制器和动作名称
动作的属性(我可以通过
HttpActionContext.ActionDescriptor.GetCustomAttributes<type>()
但在这里我没有HttpActionContext
- 我只有HttpContext
)阅读论点(例如
actionContext.ActionArguments["paramName"]
,但又一次 - 我只有一个HttpContext
)
它不是动作过滤器,也不是控制器类。但是,我可以访问HttpContext
.
解决方案
来自asp.net core 3.0 https://stackoverflow.com/a/60602828/10612695
public async Task Invoke(HttpContext context)
{
// Get the enpoint which is executing (asp.net core 3.0 only)
var executingEnpoint = context.GetEndpoint();
// Get attributes on the executing action method and it's defining controller class
var attributes = executingEnpoint.Metadata.OfType<MyCustomAttribute>();
await next(context);
// Get the enpoint which was executed (asp.net core 2.2 possible after call to await next(context))
var executingEnpoint2 = context.GetEndpoint();
// Get attributes on the executing action method and it's defining controller class
var attributes2 = executingEnpoint.Metadata.OfType<MyCustomAttribute>();
}
推荐阅读
- java - Java 13 三引号文本块 *WITHOUT* 换行符
- python - 基于随机布尔值在python中创建四叉树
- php - 与 Mcrypt 相比,使用 OpenSSL 的加密/解密导致不同的结果
- python - 对于循环 i 数据帧计数行而不是列
- node.js - 如果我访问我网站中的特定页面而不访问我的主页,则无法加载 reactjs 组件-(Node + MongoDB + express + ReactJs)
- android - 将存储库类注入 ViewModel 的最佳方法是什么?
- javascript - 在 JavaScript 中计时 if 语句
- javascript - 关于 React 组件中 .this 绑定的问题
- r - R read.csv 可以返回一个空的数据框吗?
- spring-boot - 将 Spring Boot Actuator 健康状态从 UP/DOWN 转换为 TRUE/FALSE