c#参数名ascii码从小到大排序(字典序)拼接

        /// <summary>
        /// c# 参数名ascii码从小到大排序(字典序)拼接
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        public static string AsciiDicToStr(Dictionary<string, string> dir)
        {
            string[] arrKeys = dir.Keys.ToArray();
            Array.Sort(arrKeys, string.CompareOrdinal);
            var sb = new StringBuilder();
            foreach (var key in arrKeys)
            {
                string value = dir[key];
                sb.Append(key + "=" + value + "&");
            }          
            return sb.ToString().Substring(0, sb.ToString().Length - 1);     
        }
   public ActionResult Getstr() 
        {
            Dictionary<string, string> keyValues = new Dictionary<string, string>();
            keyValues.Add("nonce", "1234abcd1234abcd");
            keyValues.Add("api", "net.charge.test");
            keyValues.Add("version", "1.0");
            keyValues.Add("data1", "aaa");
            keyValues.Add("data2", "bbb");
            string str = AsciiDicToStr(keyValues); 
            return Content(str);
        }

为您推荐

发表评论

电子邮件地址不会被公开。 必填项已用*标注