在OnActionExecuted 获取请求参数的值(包含类类型)

1.在OnActionExecuting里 获取请求参数的值 比较简单

复制代码
/// <summary>
        /// 获取首参数的值
        /// </summary>
        /// <param name="filterContext">The filter context.</param>
        /// <returns></returns>
        private object GetFirstParamObject(ActionExecutingContext filterContext)
        {
            var paramNames = filterContext.ActionDescriptor.GetParameters();
            if (paramNames.Length > 0)
            {
                var parameterInfo = filterContext.ActionParameters[paramNames[0].ParameterName];

                return parameterInfo;
            }

            return null;
        }
复制代码

 

2.在OnActionExecuted 获取请求参数的值(包含类类型)

复制代码
        /// <summary>
        /// OnActionExecuted 获取请求参数的值
        /// </summary>
        /// <param name="filterContext">filterContext</param>
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            try
            {
                //请求类各个字段的值
                Dictionary<string, string> parmsObj = new Dictionary<string, string>();

                foreach (var item in filterContext.ActionDescriptor.GetParameters())
                {
                    var itemType = item.ParameterType;
                    if (itemType.IsClass && itemType.Name != "String")
                    {
                        PropertyInfo[] infos = itemType.GetProperties();
                        foreach (PropertyInfo info in infos)
                        {
                            if (info.CanRead)
                            {
                                var propertyValue = filterContext.Controller.ValueProvider.GetValue(info.Name);// 暂不支持多层嵌套 后期优化?
                                if (!parmsObj.ContainsKey(info.Name))
                                {
                                    parmsObj.Add(info.Name, null == propertyValue ? "" : propertyValue.AttemptedValue);
                                }
                            }
                        }
                    }
                    else
                    {
                        var parameterValue = filterContext.Controller.ValueProvider.GetValue(item.ParameterName);
                        if (!parmsObj.ContainsKey(item.ParameterName))
                        {
                            parmsObj.Add(item.ParameterName, null == parameterValue ? "" : parameterValue.AttemptedValue);
                        }
                    }
                }
          }
复制代码

 

posted @   zslm___  阅读(7574)  评论(5编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2016-09-08 清除数据库的关联
点击右上角即可分享
微信分享提示