C# 获取请求(参数,表单,Header) 数据操作类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json;
using System.Reflection;
using System.Collections.Specialized;
using System.Web.Script.Serialization;
using System.Text.RegularExpressions;
namespace XiaoFeng
{
    /*
    ===================================================================
       Author : jacky
       Email : [email protected]
       QQ : 7092734
       Site : www.zhuovi.com
       Create Time : 2017/12/22 15:11:05
       Update Time : 2017/12/22 15:11:05
    ===================================================================
    */
    /// 
    /// 获取请求数据
    /// Verstion : 1.0.0
    /// Author : jacky
    /// Email : [email protected]
    /// QQ : 7092734
    /// Site : www.zhuovi.com
    /// Create Time : 2017/12/22 15:11:05
    /// Update Time : 2017/12/22 15:11:05
    /// 
    public static class TokenX
    {
        #region 属性
        /// 
        /// 请求对象
        /// 
        private static HttpRequest Request { get { return HttpContext.Current.Request; } }
        #endregion

        #region 方法

        #region 获取参数值
        /// 
        /// 获取参数值
        /// 
        /// 参数名称
        /// 默认值
        /// 
        public static String Get(string queryName = "", string defaultValue = "")
        {
            return Get(queryName, defaultValue);
        }
        /// 
        /// 获取参数值
        /// 
        /// 类型
        /// 参数名称
        /// 默认值
        /// 
        public static T Get(string queryName = "", T defaultValue = default(T))
        {
            return _Get(Request.QueryString, queryName, defaultValue);
        }
        #endregion

        #region 获取表单值
        /// 
        /// 获取表单值
        /// 
        /// 表单名称
        /// 默认值
        /// 
        public static String Post(string postName = "", string defaultValue = "")
        {
            return Post(postName, defaultValue);
        }
        /// 
        /// 获取表单值
        /// 
        /// 类型
        /// 表单名称
        /// 默认值
        /// 
        public static T Post(string postName = "", T defaultValue = default(T))
        {
            return _Get(Request.Form, postName, defaultValue);
        }
        #endregion

        #region 获取Header值
        /// 
        /// 获取Header值
        /// 
        /// Header名称
        /// 默认值
        /// 
        public static String Header(string headerName = "", string defaultValue = "")
        {
            return Header(headerName, defaultValue);
        }
        /// 
        /// 获取Header值
        /// 
        /// 类型
        /// Header名称
        /// 默认值
        /// 
        public static T Header(string headerName = "", T defaultValue = default(T))
        {
            return _Get(Request.Headers, headerName, defaultValue);
        }
        #endregion

        #region 获取值
        /// 
        /// 获取值
        /// 
        /// 类型
        /// 名称
        /// 默认值
        /// 
        public static T Value(string name = "", T defaultValue = default(T))
        {
            T val = default(T);
            val = Get(name);
            if (val == null)
            {
                val = Post(name);
                if (val == null)
                {
                    val = Header(name);
                    if (val == null) return defaultValue == null ? default(T) : defaultValue;
                    else return val;
                }
                else
                    return val;
            }
            else
                return val;
        }
        #endregion

        #region 获取键值
        /// 
        /// 获取键值
        /// 
        /// 类型
        /// 请求对象
        /// 参数名称
        /// 默认值
        /// 
        private static T _Get(NameValueCollection nameValue, string queryName = "", T defaultValue = default(T))
        {
            Type t = typeof(T);
            object QueryValue = null;
            T model = default(T);
            if (queryName.IsNullOrEmpty())
            {
                if (nameValue.AllKeys.Length == 0) return defaultValue == null ? default(T) : defaultValue;
                else QueryValue = nameValue[0];
                QueryValue = QueryValue.ToString().ReplaceSQL();
                int f = 0;
                if (t.IsValueType)
                {
                    if (t.IsEnum)
                        return QueryValue.ToString().ToEnum();
                    else if (t.Namespace == "System" && t.IsPublic)
                        return QueryValue.ToString().ToCast(defaultValue);
                    else
                        f = 1;
                }
                else if (t.Name == "String")
                    return (T)QueryValue;
                else if (t.IsClass) f = 2;
                if (f > 0)
                {
                    model = defaultValue == null ? Activator.CreateInstance() : defaultValue;
                    if (nameValue.AllKeys.Length == 1)
                    {
                        if (QueryValue.IsJson())
                        {
                            try { return JsonDeserializeObject(QueryValue.ToString()); }
                            catch {  }
                        }
                        return model;
                    }
                    else
                    {
                        nameValue.AllKeys.Each(k =>
                        {
                            string value = nameValue[k].ReplaceSQL();
                            if (value.IsNullOrEmpty()) return true;
                            PropertyInfo pi = t.GetProperty(k, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                            if (pi == null)
                            {
                                object m = model;
                                FieldInfo fi = t.GetField(k, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                                if (fi != null && fi.IsPublic)
                                {
                                    fi.SetValue(m, value);
                                    model = (T)m;
                                }
                            }
                            else
                                if (pi.CanWrite) pi.SetValue(model, value, null);
                            return true;
                        });
                        return model;
                    }
                }
            }
            else
            {
                QueryValue = (nameValue[queryName] ?? "").ReplaceSQL();
                if (QueryValue.ToString().IsNullOrEmpty()) return defaultValue == null ? default(T) : defaultValue;
                int _f = 0;
                if (t.IsValueType)
                {
                    if (t.IsEnum)
                        return QueryValue.ToString().ToEnum();
                    else if (t.Namespace == "System" && t.IsPublic)
                        return QueryValue.ToString().ToCast();
                    else
                        _f = 1;
                }
                else if (t.Name == "String")
                    return (T)QueryValue;
                else if (t.IsClass) _f = 2;
                if (_f > 0)
                {
                    model = defaultValue == null ? Activator.CreateInstance() : defaultValue;
                    if (QueryValue.IsJson())
                    {
                        try { return JsonDeserializeObject(QueryValue.ToString()); }
                        catch {  }
                    }
                    return model;
                }
            }
            return defaultValue == null ? default(T) : defaultValue;
        }
        #endregion

        #region 是否是JSON
        /// 
        /// 是否是JSON
        /// 
        /// 数据
        /// 
        private static Boolean IsJson(this object _)
        {
            if (_.ToString().isPattern(@"^(({[\s\S]+})|(\[[\s\S]+\]))$"))
            {
                try
                {
                    var serializer = new JavaScriptSerializer();
                    dynamic obj = serializer.Deserialize(_.ToString(), typeof(object));
                    return obj != null;
                }
                catch
                {
                    return false;
                }
            }
            else
                return false;
        }
        #endregion

        #region JSON
        /// 
        /// 反序列化JSON到一个对象
        /// 
        /// 对象
        /// json串
        /// 
        public static T JsonDeserializeObject(string json) { return JsonConvert.DeserializeObject(json); }
        /// 
        /// 序列化一个对象到一个JSON
        /// 
        /// 对象
        /// 
        public static string JsonSerializeObject(object o) { return JsonConvert.SerializeObject(o); }
        #endregion

        #endregion
    }
}

你可能感兴趣的:(c#)