using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Web; namespace Mtxfw.Utility { public class QueryString { private Hashtable m_HashTable; /// /// 初始化 /// public QueryString() { m_HashTable = new Hashtable(); InitUrl(); } /// /// 初始化参数列表 /// private void InitUrl() { m_HashTable.Clear(); string[] urlKeys = HttpContext.Current.Request.QueryString.AllKeys; foreach (string key in urlKeys) { string keyValue = HttpUtility.UrlDecode(HttpContext.Current.Request.QueryString[key]); m_HashTable.Add(key, keyValue); } } /// /// 返回所有参数的个数 /// /// public int Count() { if (m_HashTable != null) { return m_HashTable.Count; } return 0; } /// /// 返回String类型对应键的值 /// /// key /// 默认值 /// 键值 public string Get(string pKey, string pDefValue = "") { if (m_HashTable.ContainsKey(pKey)) { return m_HashTable[pKey].ToString().Trim(); } return pDefValue; } /// /// 返回Boolean类型的参数值 /// /// /// /// public bool Get(string pKey, bool pDefValue) { if (m_HashTable.ContainsKey(pKey)) { try { bool rtnValue = Convert.ToBoolean(m_HashTable[pKey].ToString().Trim()); return rtnValue; } catch { return pDefValue; } } return pDefValue; } /// /// 返回Decimal类型的参数值 /// /// /// /// public decimal Get(string pKey, decimal pDefValue) { try { if (m_HashTable.ContainsKey(pKey)) { return Convert.ToDecimal(m_HashTable[pKey].ToString()); } return pDefValue; } catch { return pDefValue; } } /// /// 返回double类型的参数值 /// /// /// /// public double Get(string pKey, double pDefValue) { try { if (m_HashTable.ContainsKey(pKey)) { return Convert.ToDouble(m_HashTable[pKey].ToString()); } return pDefValue; } catch { return pDefValue; } } /// /// 返回int类型的参数值 /// /// /// /// public int Get(string pKey, int pDefValue) { try { if (m_HashTable.ContainsKey(pKey)) { return Convert.ToInt32(m_HashTable[pKey].ToString()); } return pDefValue; } catch { return pDefValue; } } /// /// 返回long类型的参数值 /// /// /// /// public long Get(string pKey, long pDefValue) { try { if (m_HashTable.ContainsKey(pKey)) { return Convert.ToInt64(m_HashTable[pKey].ToString()); } return pDefValue; } catch { return pDefValue; } } /// /// 设置url参数 /// /// /// public void Set(string pKey, string pValue) { if (m_HashTable.ContainsKey(pKey)) { m_HashTable[pKey] = pValue; } else { m_HashTable.Add(pKey, pValue); } } /// /// 删除参数 /// /// public void Delete(string pKey) { if (m_HashTable.ContainsKey(pKey)) { m_HashTable.Remove(pKey); } } /// /// 获取当前页面的url参数字符串 /// /// /// public string ToQueryString(bool urlEncode = true) { string rtnValue = ""; string[] arrParams = new string[m_HashTable.Count]; int index = 0; IDictionaryEnumerator enumerator = m_HashTable.GetEnumerator(); try { while (enumerator.MoveNext()) { DictionaryEntry current = (DictionaryEntry)enumerator.Current; if (urlEncode) { arrParams[index] = string.Format("{0}={1}", current.Key.ToString(), HttpUtility.UrlEncode(current.Value.ToString())); } else { arrParams[index] = string.Format("{0}={1}", current.Key.ToString(), current.Value.ToString()); } index++; } } finally { IDisposable disposable = enumerator as IDisposable; if (disposable != null) { disposable.Dispose(); } } rtnValue = string.Join("&", arrParams); return rtnValue; } /// /// 清空数据 /// public void Clear() { m_HashTable.Clear(); } } }