首页 > 解决方案 > 如何在 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
}

标签: .net-coreaction-filteronactionexecuted

解决方案


里面有很多东西context,所以如果你在里面一探究竟,你会发现各种有用的东西。

就您而言,我认为您想要的context.ActionParameters是传递给控制器​​端点的参数集合。如果您知道您的方法应该始终接收一个且只有一个参数,您可以使用以下方式访问它:

var myId = (context.ActionParameters.Single().Value as Profile).Id

所有关于 Single() 和强制转换的典型注释和警告都适用于此处,因此请确保以适合您的情况的正确方式从集合中获取参数。


推荐阅读