.net Core 中参数的处理

获取Post和Get里的参数

    public static Hashtable GetPostParms(HttpContext context)
        {
            Hashtable param = new Hashtable();
            if(context.Request.Method.ToLower().Equals("post"))
            {
                if(context.Request.Form.Keys.Count>0)
                {
                    foreach (var key in context.Request.Form.Keys.ToList())
                    {
                        param.Add(key, context.Request.Form[key].ToString());
                    }
                }

            }
            else if (context.Request.Method.ToLower().Equals("get"))
            {
                string httpParam = context.Request.QueryString.Value;
                if(httpParam.Length>0)
                {
                string DataStr = HttpUtility.UrlDecode(httpParam);
                string str = DataStr.Substring(1, DataStr.Length-1);//去除?
                if(str.Contains("&"))//多个参数
                {
                    string[] strArray = str.Split('&');

                    for (int i = 0; i < strArray.Length; i++)
                    {
                      string key=  strArray[i].Split('=')[0];
                      string value=  strArray[i].Split('=')[1];
                        param.Add(key, value);
                    }
                }
                else
                {
                    string[] strArray = str.Split('=');
                   string key = strArray[0];
                    string value = strArray[1];
                    param.Add(key, value);
                }
            }
            else
            {
                param.Add("key", context.Request.Method);
            }
            }
            return param;
        }

过滤地址中的拼音和特殊字符

  //过滤地址字符串
        public static string FilterChar(string inputValue)
        {
            if (Regex.IsMatch(inputValue, "[A-Za-z0-9\u4e00-\u9fa5-]+"))
            {
                return Regex.Match(inputValue, "[A-Za-z0-9\u4e00-\u9fa5-]+").Value;
            }
            return "";
        }

截取字符串中的地址信息

 //address,地址字符串
        public static Dictionary<string, string> ParsingAddress(string address)
        {

            string province = string.Empty;
            string city = string.Empty;
            string country = string.Empty;
            string community = string.Empty;
            Dictionary<string, string> addres = new Dictionary<string, string>();
            int index = 0;

            StringBuilder stringBuilder = new StringBuilder();          //创建StringBuilder类对象
            string str = address;
            char[] array = str.ToCharArray();                //把字符串转化成字符数组
            IEnumerator enumerator = array.GetEnumerator();         //得到枚举器
            while (enumerator.MoveNext())                         //开始枚举
            {
                if ((char)enumerator.Current != ' ')         //向StringBuilder类对象添加非空格字符
                    stringBuilder.Append(enumerator.Current.ToString());
            }
            string area = stringBuilder.ToString();
            area = Regex.Replace(area, @"[^\u4e00-\u9fa5]", string.Empty);
            if (area.Contains("省"))
            {
                index = area.IndexOf("省");
                province = area.Substring(0, index + 1);
                addres.Add("province", province);
            }
            if (area.Contains("市"))
            {
                if (!area.Contains("省"))
                {
                    area = area.Substring(index);
                }
                else
                {
                    area = area.Substring(index + 1);
                }
                index = area.IndexOf("市");
                city = area.Substring(0, index + 1);
                addres.Add("city", city);
            }
            if (area.Contains("县") || area.Contains("区"))
            {
                index = area.LastIndexOf("市");
                area = area.Substring(index + 1);
                index = area.IndexOf("县");
                if (index == -1)
                {

                    index = area.IndexOf("区");
                    country = area.Substring(0, index + 1);
                    addres.Add("country", country);
                    index = area.LastIndexOf("区");
                    community = area.Substring(index + 1);
                    addres.Add("community", community);
                }
                else
                {
                    country = area.Substring(0, index + 1);
                    addres.Add("country", country);
                    index = area.LastIndexOf("县");
                    community = area.Substring(index + 1);
                    addres.Add("community", community);
                }

            }
            return addres;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

或与且与或非

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值