256 lines
6.9 KiB
C#
256 lines
6.9 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
/// 初始化
|
|
/// </summary>
|
|
public QueryString()
|
|
{
|
|
m_HashTable = new Hashtable();
|
|
InitUrl();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 初始化参数列表
|
|
/// </summary>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回所有参数的个数
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public int Count()
|
|
{
|
|
if (m_HashTable != null)
|
|
{
|
|
return m_HashTable.Count;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回String类型对应键的值
|
|
/// </summary>
|
|
/// <param name="pKey">key</param>
|
|
/// <param name="pDefValue">默认值</param>
|
|
/// <returns>键值</returns>
|
|
public string Get(string pKey, string pDefValue = "")
|
|
{
|
|
if (m_HashTable.ContainsKey(pKey))
|
|
{
|
|
return m_HashTable[pKey].ToString().Trim();
|
|
}
|
|
return pDefValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回Boolean类型的参数值
|
|
/// </summary>
|
|
/// <param name="pKey"></param>
|
|
/// <param name="pDefValue"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回Decimal类型的参数值
|
|
/// </summary>
|
|
/// <param name="pKey"></param>
|
|
/// <param name="pDefValue"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回double类型的参数值
|
|
/// </summary>
|
|
/// <param name="pKey"></param>
|
|
/// <param name="pDefValue"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回int类型的参数值
|
|
/// </summary>
|
|
/// <param name="pKey"></param>
|
|
/// <param name="pDefValue"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回long类型的参数值
|
|
/// </summary>
|
|
/// <param name="pKey"></param>
|
|
/// <param name="pDefValue"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置url参数
|
|
/// </summary>
|
|
/// <param name="pKey"></param>
|
|
/// <param name="pValue"></param>
|
|
public void Set(string pKey, string pValue)
|
|
{
|
|
|
|
if (m_HashTable.ContainsKey(pKey))
|
|
{
|
|
m_HashTable[pKey] = pValue;
|
|
}
|
|
else
|
|
{
|
|
m_HashTable.Add(pKey, pValue);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除参数
|
|
/// </summary>
|
|
/// <param name="pKey"></param>
|
|
public void Delete(string pKey)
|
|
{
|
|
if (m_HashTable.ContainsKey(pKey))
|
|
{
|
|
m_HashTable.Remove(pKey);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取当前页面的url参数字符串
|
|
/// </summary>
|
|
/// <param name="urlEncode"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清空数据
|
|
/// </summary>
|
|
public void Clear()
|
|
{
|
|
m_HashTable.Clear();
|
|
}
|
|
|
|
|
|
}
|
|
}
|