http 協議集合,超級簡單

時間都瘋了,你知道嗎?發表於2016-03-07

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace App.Extend
{/// <summary>
/// ReqUtils
/// </summary>
public class ReqUtils
{
/// <summary>
/// 返回HttpContext.Current.Request物件
/// </summary>
public static HttpRequest Request
{
get { return HttpContext.Current.Request; }
}

/// <summary>
/// 從Form獲取字串型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static string FormString(string name)
{
return Request.Form[name];
}
/// <summary>
/// 從Form獲取字串型別的引數,如果沒有則返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static string FormString(string name, string defaultValue)
{
string value = Request.Form[name];
return value == null ? defaultValue : value;
}
/// <summary>
/// 從Query獲取字串型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static string QueryString(string name)
{
return Request.QueryString[name];
}
/// <summary>
/// 從Query獲取字串型別的引數,如果沒有則返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static string QueryString(string name, string defaultValue)
{
string value = Request.QueryString[name];
return value == null ? defaultValue : value;
}
/// <summary>
/// 先從Query獲取引數,如果沒有再從Form獲取
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static string GetString(string name)
{
/*
string value = Request.QueryString[name];
if (value == null)
{
value = Request.Form[name];
}
return value;*/
return Param(name);
}

public static string Param(string name)
{
return Request.Params[name];
}

public static string[] GetValues(string name)
{
string[] values = Request.Params.GetValues(name);
return values == null ? new string[0] : values;
}

/// <summary>
/// 先從Form獲取引數,如果沒有再從Query獲取,如果仍然為空,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static string GetString(string name, string defaultValue)
{
string value = GetString(name);
return value == null ? defaultValue : value;
}
/// <summary>
/// 從Form獲取引數的字串陣列
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static string[] FormStringArray(string name)
{
string[] values = Request.Form.GetValues(name);
return values == null ? new string[0] : values;
}
/// <summary>
/// 從Query獲取引數的字串陣列
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static string[] QueryStringArray(string name)
{
string[] values = Request.QueryString.GetValues(name);
return values == null ? new string[0] : values;
}
/// <summary>
/// 從Form和Query獲取引數的字串陣列,返回兩個地方的總和
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static string[] GetStringArray(string name)
{
string[] formArray = FormStringArray(name);
string[] queryArray = QueryStringArray(name);
string[] strArray = new string[formArray.Length + queryArray.Length];
Array.ConstrainedCopy(formArray, 0, strArray, 0, formArray.Length);
Array.ConstrainedCopy(queryArray, 0, strArray, formArray.Length, queryArray.Length);
return strArray;
}

/*================Boolean型別引數獲取================*/
/// <summary>
/// 從Form獲取bool型別的引數 支援"True" "true" "False" "false"
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static bool FormBool(string name)
{
string str = FormString(name);
if (string.IsNullOrEmpty(str)) throw new Exception("沒有名為“" + name + "”的引數!");
if ("True" != str && "true" != str && "1" != str && "False" != str && "false" != str && "0" != str) throw new Exception("錯誤的Boolean表示式!");
return "True" == str || "true" == str || "1" == str;
}
/// <summary>
/// 從Form獲取bool型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static bool FormBool(string name, bool defaultValue)
{
try
{
return FormBool(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Form獲取bool型別的引數 支援"True" "true" "False" "false"
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static bool? FormNullableBool(string name)
{
string str = FormString(name);
if (string.IsNullOrEmpty(str)) return null;
if ("True" != str && "true" != str && "1" != str && "False" != str && "false" != str && "0" != str) throw new Exception("錯誤的Boolean表示式!");
return "True" == str || "true" == str || "1" == str;
}
/// <summary>
/// 從Form獲取bool型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static bool? FormNullableBool(string name, bool? defaultValue)
{
try
{
return FormNullableBool(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Query獲取int型別的引數 支援"True" "true" "False" "false"
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static bool QueryBool(string name)
{
string str = QueryString(name);
if (string.IsNullOrEmpty(str)) throw new Exception("沒有名為“" + name + "”的引數!");
if ("True" != str && "true" != str && "1" != str && "False" != str && "false" != str && "0" != str) throw new Exception("錯誤的Boolean表示式!");
return "True" == str || "true" == str || "1" == str;
}
/// <summary>
/// 從Query獲取bool型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static bool QueryBool(string name, bool defaultValue)
{
try
{
return QueryBool(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Query獲取int型別的引數 支援"True" "true" "False" "false"
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static bool? QueryNullableBool(string name)
{
string str = QueryString(name);
if (string.IsNullOrEmpty(str)) return null;
if ("True" != str && "true" != str && "1" != str && "False" != str && "false" != str && "0" != str) throw new Exception("錯誤的Boolean表示式!");
return "True" == str || "true" == str || "1" == str;
}
/// <summary>
/// 從Query獲取bool型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static bool? QueryNullableBool(string name, bool? defaultValue)
{
try
{
return QueryNullableBool(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Form獲取bool引數的陣列
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static bool[] FormBoolArray(string name)
{
string[] strArray = FormStringArray(name);
return Array.ConvertAll<string, bool>(strArray, delegate(string str)
{
if ("True" != str && "true" != str && "1" != str && "False" != str && "false" != str && "0" != str) throw new Exception("錯誤的Boolean表示式!");
return "True" == str || "true" == str || "1" == str;
});
}
/// <summary>
/// 從Query獲取bool引數的陣列
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static bool[] QueryBoolArray(string name)
{
string[] strArray = QueryStringArray(name);
return Array.ConvertAll<string, bool>(strArray, delegate(string str)
{
if ("True" != str && "true" != str && "1" != str && "False" != str && "false" != str && "0" != str) throw new Exception("錯誤的Boolean表示式!");
return "True" == str || "true" == str || "1" == str;
});
}

/// <summary>
/// 從Form和Query獲取bool型別的引數 支援"True" "true" "False" "false"
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static bool GetBool(string name)
{
string str = GetString(name);
if (string.IsNullOrEmpty(str)) throw new Exception("沒有名為“" + name + "”的引數!");
if ("True" != str && "true" != str && "1" != str && "False" != str && "false" != str && "0" != str) throw new Exception("錯誤的Boolean表示式!");
return "True" == str || "true" == str || "1" == str;
}
/// <summary>
/// 從Form和Query獲取bool型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static bool GetBool(string name, bool defaultValue)
{
try
{
return GetBool(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Form和Query獲取bool型別的引數 支援"True" "true" "False" "false"
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static bool? GetNullableBool(string name)
{
string str = GetString(name);
if (string.IsNullOrEmpty(str)) return null;
if ("True" != str && "true" != str && "1" != str && "False" != str && "false" != str && "0" != str) throw new Exception("錯誤的Boolean表示式!");
return "True" == str || "true" == str || "1" == str;
}
/// <summary>
/// 從Form和Query獲取bool型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static bool? GetNullableBool(string name, bool? defaultValue)
{
try
{
return GetNullableBool(name);
}
catch
{
return defaultValue;
}
}
/*================Int型別引數獲取================*/
/// <summary>
/// 從Form獲取int型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static int FormInt(string name)
{
string str = FormString(name);
if (string.IsNullOrEmpty(str)) throw new Exception("沒有名為“" + name + "”的引數!");
return Convert.ToInt32(str);
}
/// <summary>
/// 從Form獲取int型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static int FormInt(string name, int defaultValue)
{
try
{
return FormInt(name);
}
catch
{
return defaultValue;
}
}

/// <summary>
/// 從Form獲取int型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static int? FormNullableInt(string name)
{
string str = FormString(name);
if (string.IsNullOrEmpty(str)) return null;
return Convert.ToInt32(str);
}
/// <summary>
/// 從Form獲取int型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static int? FormNullableInt(string name, int? defaultValue)
{
try
{
return FormNullableInt(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Query獲取int型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static int QueryInt(string name)
{
string str = QueryString(name);
if (string.IsNullOrEmpty(str)) throw new Exception("沒有名為“" + name + "”的引數!");
return Convert.ToInt32(str);
}
/// <summary>
/// 從Query獲取int型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static int QueryInt(string name, int defaultValue)
{
try
{
return QueryInt(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Query獲取int型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static int? QueryNullableInt(string name)
{
string str = QueryString(name);
if (string.IsNullOrEmpty(str)) return null;
return Convert.ToInt32(str);
}
/// <summary>
/// 從Query獲取int型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static int? QueryNullableInt(string name, int? defaultValue)
{
try
{
return QueryNullableInt(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Form獲取int引數的陣列
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static int[] FormIntArray(string name)
{
string[] strArray = FormStringArray(name);
return Array.ConvertAll<string, int>(strArray, delegate(string str)
{
return Convert.ToInt32(str);
});
}
/// <summary>
/// 從Query獲取int引數的陣列
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static int[] QueryIntArray(string name)
{
string[] strArray = QueryStringArray(name);
return Array.ConvertAll<string, int>(strArray, delegate(string str)
{
return Convert.ToInt32(str);
});
}

/// <summary>
/// 從query和form獲取int陣列
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static int[] GetIntArray(string name)
{
string[] strArray = GetValues(name);
if(strArray==null)return new int[0];
return Array.ConvertAll<string, int>(strArray, delegate(string str)
{
return Convert.ToInt32(str);
});
}

/// <summary>
/// 從Form和Query獲取int型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static int GetInt(string name)
{
string str = GetString(name);
if (string.IsNullOrEmpty(str)) throw new Exception("沒有名為“" + name + "”的引數!");
return Convert.ToInt32(str);
}
/// <summary>
/// 從Form和Query獲取int型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static int GetInt(string name, int defaultValue)
{
try
{
return GetInt(name);
}
catch
{
return defaultValue;
}
}

/// <summary>
/// 從Form和Query獲取int型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static int? GetNullableInt(string name)
{
string str = GetString(name);
if (string.IsNullOrEmpty(str)) return null;
return Convert.ToInt32(str);
}
/// <summary>
/// 從Form和Query獲取int型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static int? GetNullableInt(string name, int? defaultValue)
{
try
{
return GetNullableInt(name);
}
catch
{
return defaultValue;
}
}

/*================Long型別引數獲取================*/
/// <summary>
/// 從Form獲取long 型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static long FormLong(string name)
{
string str = FormString(name);
if (string.IsNullOrEmpty(str)) throw new Exception("沒有名為“" + name + "”的引數!");
return Convert.ToInt64(str);
}
/// <summary>
/// 從Form獲取long 型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static long FormLong(string name, long defaultValue)
{
try
{
return FormLong(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Form獲取long 型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static long? FormNullableLong(string name)
{
string str = FormString(name);
if (string.IsNullOrEmpty(str)) return null;
return Convert.ToInt64(str);
}
/// <summary>
/// 從Form獲取long 型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static long? FormNullableLong(string name, long? defaultValue)
{
try
{
return FormNullableLong(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Query獲取long 型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static long QueryLong(string name)
{
string str = QueryString(name);
if (string.IsNullOrEmpty(str)) throw new Exception("沒有名為“" + name + "”的引數!");
return Convert.ToInt64(str);
}
/// <summary>
/// 從Query獲取long 型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static long QueryLong(string name, long defaultValue)
{
try
{
return QueryLong(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Query獲取long 型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static long? QueryNullableLong(string name)
{
string str = QueryString(name);
if (string.IsNullOrEmpty(str)) return null;
return Convert.ToInt64(str);
}
/// <summary>
/// 從Query獲取long 型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static long? QueryNullableLong(string name, long? defaultValue)
{
try
{
return QueryNullableLong(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Form獲取long型別引數陣列
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static long[] FormLongArray(string name)
{
string[] strArray = FormStringArray(name);
return Array.ConvertAll<string, long>(strArray, delegate(string str)
{
return Convert.ToInt64(str);
});
}
/// <summary>
/// 從Query獲取long型別引數陣列
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static long[] QueryLongArray(string name)
{
string[] strArray = QueryStringArray(name);
return Array.ConvertAll<string, long>(strArray, delegate(string str)
{
return Convert.ToInt64(str);
});
}

/// <summary>
/// 從query和form獲取long陣列
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static long[] GetLongArray(string name)
{
string[] strArray = GetValues(name);
return Array.ConvertAll<string, long>(strArray, delegate(string str)
{
return Convert.ToInt64(str);
});
}
/// <summary>
/// 從Form和Query獲取long 型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static long GetLong(string name)
{
string str = GetString(name);
if (string.IsNullOrEmpty(str)) throw new Exception("沒有名為“" + name + "”的引數!");
return Convert.ToInt64(str);
}
/// <summary>
/// 從Form和Query獲取long 型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static long GetLong(string name, long defaultValue)
{
try
{
return GetLong(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Form和Query獲取long 型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static long? GetNullableLong(string name)
{
string str = GetString(name);
if (string.IsNullOrEmpty(str)) return null;
return Convert.ToInt64(str);
}
/// <summary>
/// 從Form和Query獲取long 型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static long? GetNullableLong(string name, long? defaultValue)
{
try
{
return GetNullableLong(name);
}
catch
{
return defaultValue;
}
}
/*================Float型別引數獲取================*/
/// <summary>
/// 從Form獲取float型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static float FormFloat(string name)
{
string str = FormString(name);
if (string.IsNullOrEmpty(str)) throw new Exception("沒有名為“" + name + "”的引數!");
return float.Parse(str);
}
/// <summary>
/// 從Form獲取float型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static float FormFloat(string name, float defaultValue)
{
try
{
return FormFloat(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Form獲取float型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static float? FormNullableFloat(string name)
{
string str = FormString(name);
if (string.IsNullOrEmpty(str)) return null;
return float.Parse(str);
}
/// <summary>
/// 從Form獲取float型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static float? FormNullableFloat(string name, float? defaultValue)
{
try
{
return FormNullableFloat(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Query獲取float型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static float QueryFloat(string name)
{
string str = QueryString(name);
if (string.IsNullOrEmpty(str)) throw new Exception("沒有名為“" + name + "”的引數!");
return float.Parse(str);
}
/// <summary>
/// 從Query獲取float型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static float QueryFloat(string name, float defaultValue)
{
try
{
return QueryFloat(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Query獲取float型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static float? QueryNullableFloat(string name)
{
string str = QueryString(name);
if (string.IsNullOrEmpty(str)) return null;
return float.Parse(str);
}
/// <summary>
/// 從Query獲取float型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static float? QueryNullableFloat(string name, float? defaultValue)
{
try
{
return QueryNullableFloat(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Form獲取float引數陣列
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static float[] FormFloatArray(string name)
{
string[] strArray = FormStringArray(name);
return Array.ConvertAll<string, float>(strArray, delegate(string str)
{
return float.Parse(str);
});
}
/// <summary>
/// 從Query獲取float引數陣列
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static float[] QueryFloatArray(string name)
{
string[] strArray = QueryStringArray(name);
return Array.ConvertAll<string, float>(strArray, delegate(string str)
{
return float.Parse(str);
});
}

/// <summary>
/// 從Form和Query獲取float型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static float GetFloat(string name)
{
string str = GetString(name);
if (string.IsNullOrEmpty(str)) throw new Exception("沒有名為“" + name + "”的引數!");
return float.Parse(str);
}
/// <summary>
/// 從Form和Query獲取float型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static float GetFloat(string name, float defaultValue)
{
try
{
return GetFloat(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Form和Query獲取float型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static float? GetNullableFloat(string name)
{
string str = GetString(name);
if (string.IsNullOrEmpty(str)) return null;
return float.Parse(str);
}
/// <summary>
/// 從Form和Query獲取float型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static float? GetNullableFloat(string name, float? defaultValue)
{
try
{
return GetNullableFloat(name);
}
catch
{
return defaultValue;
}
}

/*================Double型別引數獲取================*/
/// <summary>
/// 從Form獲取double型別引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static double FormDouble(string name)
{
string str = FormString(name);
if (string.IsNullOrEmpty(str)) throw new Exception("沒有名為“" + name + "”的引數!");
return double.Parse(str);
}
/// <summary>
/// 從Form獲取double型別引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static double FormDouble(string name, double defaultValue)
{
try
{
return FormDouble(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Form獲取double型別引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static double? FormNullableDouble(string name)
{
string str = FormString(name);
if (string.IsNullOrEmpty(str)) return null;
return double.Parse(str);
}
/// <summary>
/// 從Form獲取double型別引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static double? FormNullableDouble(string name, double? defaultValue)
{
try
{
return FormNullableDouble(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Query獲取double型別引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static double QueryDouble(string name)
{
string str = QueryString(name);
if (string.IsNullOrEmpty(str)) throw new Exception("沒有名為“" + name + "”的引數!");
return double.Parse(str);
}
/// <summary>
/// 從Query獲取double型別引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static double QueryDouble(string name, double defaultValue)
{
try
{
return QueryDouble(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Query獲取double型別引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static double? QueryNullableDouble(string name)
{
string str = QueryString(name);
if (string.IsNullOrEmpty(str)) return null;
return double.Parse(str);
}
/// <summary>
/// 從Query獲取double型別引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static double? QueryNullableDouble(string name, double? defaultValue)
{
try
{
return QueryNullableDouble(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Form獲取double型別引數的陣列
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static double[] FormDoubleArray(string name)
{
string[] strArray = FormStringArray(name);
return Array.ConvertAll<string, double>(strArray, delegate(string str)
{
return double.Parse(str);
});
}
/// <summary>
/// 從Query獲取double型別的引數陣列
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static double[] QueryDoubleArray(string name)
{
string[] strArray = QueryStringArray(name);
return Array.ConvertAll<string, double>(strArray, delegate(string str)
{
return double.Parse(str);
});
}

/// <summary>
/// 從Form和Query獲取double型別引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static double GetDouble(string name)
{
string str = GetString(name);
if (string.IsNullOrEmpty(str)) throw new Exception("沒有名為“" + name + "”的引數!");
return double.Parse(str);
}
/// <summary>
/// 從Form和Query獲取double型別引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static double GetDouble(string name, double defaultValue)
{
try
{
return GetDouble(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Form和Query獲取double型別引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static double? GetNullableDouble(string name)
{
string str = GetString(name);
if (string.IsNullOrEmpty(str)) return null;
return double.Parse(str);
}
/// <summary>
/// 從Form和Query獲取double型別引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static double? GetNullableDouble(string name, double? defaultValue)
{
try
{
return GetNullableDouble(name);
}
catch
{
return defaultValue;
}
}

/*================Decimal型別============================*/
/// <summary>
/// 從Form獲取decimal型別引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static decimal FormDecimal(string name)
{
string str = FormString(name);
if (string.IsNullOrEmpty(str)) throw new Exception("沒有名為“" + name + "”的引數!");
return decimal.Parse(str);
}
/// <summary>
/// 從Form獲取decimal型別引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static decimal FormDecimal(string name, decimal defaultValue)
{
try
{
return FormDecimal(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Form獲取decimal型別引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static decimal? FormNullableDecimal(string name)
{
string str = FormString(name);
if (string.IsNullOrEmpty(str)) return null;
return decimal.Parse(str);
}
/// <summary>
/// 從Form獲取decimal型別引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static decimal? FormNullableDecimal(string name, decimal? defaultValue)
{
try
{
return FormNullableDecimal(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Query獲取decimal型別引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static decimal QueryDecimal(string name)
{
string str = QueryString(name);
if (string.IsNullOrEmpty(str)) throw new Exception("沒有名為“" + name + "”的引數!");
return decimal.Parse(str);
}
/// <summary>
/// 從Query獲取decimal型別引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static decimal QueryDecimal(string name, decimal defaultValue)
{
try
{
return QueryDecimal(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Query獲取decimal型別引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static decimal? QueryNullableDecimal(string name)
{
string str = QueryString(name);
if (string.IsNullOrEmpty(str)) return null;
return decimal.Parse(str);
}
/// <summary>
/// 從Query獲取decimal型別引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static decimal? QueryNullableDecimal(string name, decimal? defaultValue)
{
try
{
return QueryNullableDecimal(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Form獲取decimal型別引數的陣列
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static decimal[] FormDecimalArray(string name)
{
string[] strArray = FormStringArray(name);
return Array.ConvertAll<string, decimal>(strArray, delegate(string str)
{
return decimal.Parse(str);
});
}
/// <summary>
/// 從Query獲取decimal型別的引數陣列
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static decimal[] QueryDecimalArray(string name)
{
string[] strArray = QueryStringArray(name);
return Array.ConvertAll<string, decimal>(strArray, delegate(string str)
{
return decimal.Parse(str);
});
}

/// <summary>
/// 從Form和Query獲取decimal型別引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static decimal GetDecimal(string name)
{
string str = GetString(name);
if (string.IsNullOrEmpty(str)) throw new Exception("沒有名為“" + name + "”的引數!");
return decimal.Parse(str);
}
/// <summary>
/// 從Form和Query獲取decimal型別引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static decimal GetDecimal(string name, decimal defaultValue)
{
try
{
return GetDecimal(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Form和Query獲取decimal型別引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static decimal? GetNullableDecimal(string name)
{
string str = GetString(name);
if (string.IsNullOrEmpty(str)) return null;
return decimal.Parse(str);
}
/// <summary>
/// 從Form和Query獲取decimal型別引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static decimal? GetNullableDecimal(string name, decimal? defaultValue)
{
try
{
return GetNullableDecimal(name);
}
catch
{
return defaultValue;
}
}


/*================DateTime型別引數獲取================*/
/// <summary>
/// 從Form獲取DateTime型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static DateTime FormDateTime(string name)
{
string str = FormString(name);
if (string.IsNullOrEmpty(str)) throw new Exception("沒有名為“" + name + "”的引數!");
return DateTime.Parse(str);
}
/// <summary>
/// 從Form獲取DateTime型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static DateTime FormDateTime(string name, DateTime defaultValue)
{
try
{
return FormDateTime(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Form獲取DateTime型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static DateTime? FormNullableDateTime(string name)
{
string str = FormString(name);
if (string.IsNullOrEmpty(str)) return null;
return DateTime.Parse(str);
}
/// <summary>
/// 從Form獲取DateTime型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static DateTime? FormNullableDateTime(string name, DateTime? defaultValue)
{
try
{
return FormNullableDateTime(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Query獲取DateTime型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static DateTime QueryDateTime(string name)
{
string str = QueryString(name);
if (string.IsNullOrEmpty(str)) throw new Exception("沒有名為“" + name + "”的引數!");
return DateTime.Parse(str);
}
/// <summary>
/// 從Query獲取DateTime型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static DateTime QueryDateTime(string name, DateTime defaultValue)
{
try
{
return QueryDateTime(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Query獲取DateTime型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static DateTime? QueryNullableDateTime(string name)
{
string str = QueryString(name);
if (string.IsNullOrEmpty(str)) return null;
return DateTime.Parse(str);
}
/// <summary>
/// 從Query獲取DateTime型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static DateTime? QueryNullableDateTime(string name, DateTime? defaultValue)
{
try
{
return QueryNullableDateTime(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Form獲取DateTime型別引數陣列
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static DateTime[] FormDateTimeArray(string name)
{
string[] strArray = FormStringArray(name);
return Array.ConvertAll<string, DateTime>(strArray, delegate(string str)
{
return DateTime.Parse(str);
});
}
/// <summary>
/// 從Query獲取DateTime型別引數陣列
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static DateTime[] QueryDateTimeArray(string name)
{
string[] strArray = QueryStringArray(name);
return Array.ConvertAll<string, DateTime>(strArray, delegate(string str)
{
return DateTime.Parse(str);
});
}

/// <summary>
/// 從Form獲取DateTime?型別的引數陣列
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static DateTime?[] FormNullableDateTimeArray(string name)
{
string[] strArray = FormStringArray(name);
return Array.ConvertAll<string, DateTime?>(strArray, delegate(string str)
{
if (string.IsNullOrEmpty(str)) return null;
return DateTime.Parse(str);
});
}
/// <summary>
/// 從Query獲取DateTime?型別的引數陣列
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static DateTime?[] QueryNullableDateTimeArray(string name)
{
string[] strArray = QueryStringArray(name);
return Array.ConvertAll<string, DateTime?>(strArray, delegate(string str)
{
if (string.IsNullOrEmpty(str)) return null;
return DateTime.Parse(str);
});
}


/// <summary>
/// 從Form和Query獲取DateTime型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static DateTime GetDateTime(string name)
{
string str = GetString(name);
if (string.IsNullOrEmpty(str)) throw new Exception("沒有名為“" + name + "”的引數!");
return DateTime.Parse(str);
}
/// <summary>
/// 從Form和Query獲取DateTime型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static DateTime GetDateTime(string name, DateTime defaultValue)
{
try
{
return GetDateTime(name);
}
catch
{
return defaultValue;
}
}
/// <summary>
/// 從Form和Query獲取DateTime型別的引數
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static DateTime? GetNullableDateTime(string name)
{
string str = GetString(name);
if (string.IsNullOrEmpty(str)) return null;
return DateTime.Parse(str);
}
/// <summary>
/// 從Form和Query獲取DateTime型別的引數,如果獲取或轉換失敗,返回預設值
/// </summary>
/// <param name="name"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static DateTime? GetNullableDateTime(string name, DateTime? defaultValue)
{
try
{
return GetNullableDateTime(name);
}
catch
{
return defaultValue;
}
}

/*================HttpPostedFile型別引數獲取================*/
/// <summary>
/// 獲取指定引數名的檔案
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static HttpPostedFile GetPostedFile(string name)
{
return Request.Files[name];
}
/// <summary>
/// 獲取指定引數名的檔案陣列
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static HttpPostedFile[] GetPostedFileArray(string name)
{
List<HttpPostedFile> list = new List<HttpPostedFile>();
string[] names = Request.Files.AllKeys;
for (int i = 0; i < names.Length; i++)
{
if (names[i] == name)
{
list.Add(Request.Files.Get(i));
}
}
return list.ToArray();
}

/// <summary>
/// 獲得當前頁面客戶端的IP
/// </summary>
/// <returns>當前頁面客戶端的IP</returns>
public static string GetIp()
{
string result = String.Empty;

result = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(result))
{
result = Request.ServerVariables["REMOTE_ADDR"];
}

if (string.IsNullOrEmpty(result))
{
result = Request.UserHostAddress;
}

if (string.IsNullOrEmpty(result))
{
return "127.0.0.1";
}

return result;
}

/// <summary>
/// 得到主機頭
/// </summary>
/// <returns></returns>
public static string GetHost()
{
return Request.Url.Host;
}

/// <summary>
/// 獲得當前完整Url地址
/// </summary>
/// <returns>當前完整Url地址</returns>
public static string GetUrl()
{
return Request.Url.ToString();
}

/// <summary>
/// 判斷當前頁面是否接收到了Post請求
/// </summary>
/// <returns>是否接收到了Post請求</returns>
public static bool IsPost()
{
return Request.HttpMethod.Equals("POST");
}

/// <summary>
/// 判斷當前頁面是否接收到了Get請求
/// </summary>
/// <returns>是否接收到了Get請求</returns>
public static bool IsGet()
{
return Request.HttpMethod.Equals("GET");
}
}
}

相關文章