Nancy获取Request.Form的所有参数

Posted luol

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nancy获取Request.Form的所有参数相关的知识,希望对你有一定的参考价值。

Nancy是一个轻量级的webapi框架,在用Nancy框架的时候,我们经常要获取到接口的所有动态参数值,

但是Nancy的Request.Form不能够直接转成NameValueCollection。

在这里,我先将Request.Form序列化成json格式字符串,在反序列化成List集合,来进行获取所有的key,

从而用key获取到所有的参数的值,存入字典类中。

下面插入方法代码:

public Dictionary<string, string> GetRequestPost()
        {
            List<string> list = JsonDynamicUtil.ToList(JsonDynamicUtil.ToJson(Request.Form));
            Dictionary<string, string> dic = new Dictionary<string, string>();
            foreach (var iten in list)
            {
                dic.Add(iten, Request.Form[iten]);
            }
            return dic;
        }

下面是需要用到的序列化对象操作类:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace Base
{
    public class JsonDynamicUtil
    {
        public static dynamic ToDynamic(string json)
        {
            javascriptSerializer jss = new JavaScriptSerializer();
            jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() });
            dynamic dy = jss.Deserialize(json, typeof(object)) as dynamic;
            return dy;
        }
        public static string ToJson(dynamic d)
        {
            JavaScriptSerializer oSerializer = new JavaScriptSerializer();
            oSerializer.RegisterConverters(new JavaScriptConverter[] { new ExpandoJSONConverter() });
            string json = oSerializer.Serialize(d);
            return json;
        }

        public static List<string> ToList(string json)
        {
            JavaScriptSerializer jss = new JavaScriptSerializer();
            jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() });
            List<string> dy = jss.Deserialize(json, typeof(List<string>)) as List<string>;
            return dy;
        }
    }
    public class DynamicJsonConverter : JavaScriptConverter
    {
        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            if (dictionary == null)
                throw new ArgumentNullException("dictionary");

            if (type == typeof(object))
            {
                return new DynamicJsonObject(dictionary);
            }

            return null;
        }

        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            throw new NotImplementedException();
        }

        public override IEnumerable<Type> SupportedTypes
        {
            get { return new ReadOnlyCollection<Type>(new List<Type>(new Type[] { typeof(object) })); }
        }
    }
    public class DynamicJsonObject : DynamicObject
    {
        private IDictionary<string, object> Dictionary { get; set; }

        public DynamicJsonObject(IDictionary<string, object> dictionary)
        {
            this.Dictionary = dictionary;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = this.Dictionary[binder.Name];

            if (result is IDictionary<string, object>)
            {
                result = new DynamicJsonObject(result as IDictionary<string, object>);
            }
            else if (result is ArrayList && (result as ArrayList) is IDictionary<string, object>)
            {
                result = new List<DynamicJsonObject>((result as ArrayList).ToArray().Select(x => new DynamicJsonObject(x as IDictionary<string, object>)));
            }
            else if (result is ArrayList)
            {
                result = new List<object>((result as ArrayList).ToArray());
            }

            return this.Dictionary.ContainsKey(binder.Name);
        }
    }
    public class ExpandoJSONConverter : JavaScriptConverter
    {
        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            throw new NotImplementedException();
        }

        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            var result = new Dictionary<string, object>();
            var dictionary = obj as IDictionary<string, object>;

            foreach (var item in dictionary)
                result.Add(item.Key, item.Value);

            return result;
        }

        public override IEnumerable<Type> SupportedTypes
        {
            get
            {
                return new ReadOnlyCollection<Type>(new Type[] { typeof(System.Dynamic.ExpandoObject) });
            }
        }
    }
}

 如果你有更好的获取方式,希望可以留言指点一下,谢谢

参考:

https://blog.csdn.net/jingvencn/article/details/41868479

 

以上是关于Nancy获取Request.Form的所有参数的主要内容,如果未能解决你的问题,请参考以下文章

request.form.get总是返回默认值

C# 获取POST方式提交的参数,Request.Form["参数名"] 获取不到,可能是啥问题

request.form()和request()的区别

Request.From方法的功能是

无法使用来自axios请求的request.form在flask api中获取请求正文[重复]

soapui里 paramtype =form 类型参数怎么传值