获取HttpActionContext请求参数

来源:互联网 发布:小天才早教机软件下载 编辑:程序博客网 时间:2024/06/03 14:30

主要通过反射获取请求参数,输出JSON格式数据方便以后直接转换对象

string postStr ="";HttpActionContext actionContext = this.HttpActionContext; var test = actionContext.ActionArguments;                                foreach (var b in test)                                {                                    var post = actionContext.ActionArguments[b.Key];                                    if (null != post)                                    {                                        Type t = post.GetType();                                        var typeArr = t.GetProperties();                                        var str = "";                                        foreach (var a in typeArr.OrderBy(x => x.Name))                                        {                                            var n = a.Name;                                            var v = a.GetValue(post, null);                                            if (null != v && v.ToString() != "")                                            {                                                str += @"""" + n + @""":" + @"""" + v + @""",";                                            }                                        }                                        str = str.TrimEnd(',');                                        str = @"{" + str + "}";                                        postStr += str+",";                                    }                                }                                postStr.TrimEnd(',');
原创粉丝点击