.net-core - 如何在 OnActionExecuted 上获取动作模型上的属性值
问题描述
我需要在基于动作模型中的属性使用ActionFilter执行动作后运行一些代码,如何获取 OnActionExecuted 中的属性值?
模型
public class Profile
{
public Guid Id { get; set; }
public string Name { get; set; }
}
行动
public BotProfileDTO Update(Profile profile)
{
}
动作过滤器
public override void OnActionExecuted(ActionExecutedContext context)
{
//How to get profile Id here
}
解决方案
里面有很多东西context
,所以如果你在里面一探究竟,你会发现各种有用的东西。
就您而言,我认为您想要的context.ActionParameters
是传递给控制器端点的参数集合。如果您知道您的方法应该始终接收一个且只有一个参数,您可以使用以下方式访问它:
var myId = (context.ActionParameters.Single().Value as Profile).Id
所有关于 Single() 和强制转换的典型注释和警告都适用于此处,因此请确保以适合您的情况的正确方式从集合中获取参数。
推荐阅读
- javascript - 每次操作后清除 Redux Store
- python - Python os.system 返回 103
- ios - swift 4将具有未知根名称的json解码为表视图
- excel - excel的条件公式
- controller - 如何按顺序点击多个页面 || JMeter
- reactjs - 如何为 jspdf + html2canvas 添加微调器
- scala - scalaj etag get 无法解析
- sql - 查询的假脱机文件如何显示结果?(遇到麻烦)
- openmdao - OpenMDAO:如何处理 ExplicitComponent.compute 中的非收敛点?
- firebase - 从 Firebase 到 BigQuery 的实时或每小时数据同步