ASP.NetReuqest要求共通操作

17041 ワード

会社プロジェクトからのコード
using System;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;

namespace DTcms.Common
{
	/// <summary>
	/// Request   
	/// </summary>
	public class DTRequest
	{
		/// <summary>
		///             Post  
		/// </summary>
		/// <returns>      Post  </returns>
		public static bool IsPost()
		{
			return HttpContext.Current.Request.HttpMethod.Equals("POST");
		}

		/// <summary>
		///             Get  
		/// </summary>
		/// <returns>      Get  </returns>
		public static bool IsGet()
		{
			return HttpContext.Current.Request.HttpMethod.Equals("GET");
		}

		/// <summary>
		///             
		/// </summary>
		/// <param name="strName">      </param>
		/// <returns>       </returns>
		public static string GetServerString(string strName)
		{
			if (HttpContext.Current.Request.ServerVariables[strName] == null)
				return "";

            return HttpContext.Current.Request.ServerVariables[strName].ToString();
		}

		/// <summary>
		///           
		/// </summary>
		/// <returns>        </returns>
		public static string GetUrlReferrer()
		{
			string retVal = null;
    
			try
			{
				retVal = HttpContext.Current.Request.UrlReferrer.ToString();
			}
			catch{}
			
			if (retVal == null)
				return "";
    
			return retVal;
		}
		
		/// <summary>
		///          
		/// </summary>
		/// <returns></returns>
		public static string GetCurrentFullHost()
		{
			HttpRequest request = System.Web.HttpContext.Current.Request;
			if (!request.Url.IsDefaultPort)
				return string.Format("{0}:{1}", request.Url.Host, request.Url.Port.ToString());

            return request.Url.Host;
		}

		/// <summary>
		///      
		/// </summary>
		public static string GetHost()
		{
			return HttpContext.Current.Request.Url.Host;
		}

        /// <summary>
        ///      
        /// </summary>
        public static string GetDnsSafeHost()
        {
            return HttpContext.Current.Request.Url.DnsSafeHost;
        }

		/// <summary>
		///           URL(URL          ,       (    ))
		/// </summary>
		/// <returns>   URL</returns>
		public static string GetRawUrl()
		{
			return HttpContext.Current.Request.RawUrl;
		}

		/// <summary>
		///                
		/// </summary>
		/// <returns>             </returns>
		public static bool IsBrowserGet()
		{
			string[] BrowserName = {"ie", "opera", "netscape", "mozilla", "konqueror", "firefox"};
			string curBrowser = HttpContext.Current.Request.Browser.Type.ToLower();
			for (int i = 0; i < BrowserName.Length; i++)
			{
				if (curBrowser.IndexOf(BrowserName[i]) >= 0)
					return true;
			}
			return false;
		}

		/// <summary>
		///             
		/// </summary>
		/// <returns>          </returns>
		public static bool IsSearchEnginesGet()
		{
            if (HttpContext.Current.Request.UrlReferrer == null)
                return false;

            string[] SearchEngine = {"google", "yahoo", "msn", "baidu", "sogou", "sohu", "sina", "163", "lycos", "tom", "yisou", "iask", "soso", "gougou", "zhongsou"};
			string tmpReferrer = HttpContext.Current.Request.UrlReferrer.ToString().ToLower();
			for (int i = 0; i < SearchEngine.Length; i++)
			{
				if (tmpReferrer.IndexOf(SearchEngine[i]) >= 0)
					return true;
			}
			return false;
		}

		/// <summary>
		///       Url  
		/// </summary>
		/// <returns>    Url  </returns>
		public static string GetUrl()
		{
			return HttpContext.Current.Request.Url.ToString();
		}

		/// <summary>
		///     Url    
		/// </summary>
		/// <param name="strName">Url  </param>
		/// <returns>Url    </returns>
		public static string GetQueryString(string strName)
		{
            return GetQueryString(strName, false);
		}

        /// <summary>
        ///     Url    
        /// </summary> 
        /// <param name="strName">Url  </param>
        /// <param name="sqlSafeCheck">    SQL    </param>
        /// <returns>Url    </returns>
        public static string GetQueryString(string strName, bool sqlSafeCheck)
        {
            if (HttpContext.Current.Request.QueryString[strName] == null)
                return "";

            if (sqlSafeCheck && !Utils.IsSafeSqlString(HttpContext.Current.Request.QueryString[strName]))
                return "unsafe string";

            return HttpContext.Current.Request.QueryString[strName];
        }


        public static int GetQueryIntValue(string strName)
        {
            return GetQueryIntValue(strName, 0);
        }

        /// <summary>
        ///     URL    (Int )
        /// </summary>
        /// <param name="strName">URL  </param>
        /// <param name="defaultvalue">   </param>
        /// <returns>    URL    </returns>
        public static int GetQueryIntValue(string strName, int defaultvalue)
        {
            if (HttpContext.Current.Request.QueryString[strName] == null || HttpContext.Current.Request.QueryString[strName].ToString() == string.Empty)
                return defaultvalue;
            else
            {
                Regex obj = new Regex("\\d+");
                Match objmach = obj.Match(HttpContext.Current.Request.QueryString[strName].ToString());
                if (objmach.Success)
                    return Convert.ToInt32(objmach.Value);
                else
                    return defaultvalue;
            }
        }


        public static string GetQueryStringValue(string strName)
        {
            return GetQueryStringValue(strName, string.Empty);
        }

        /// <summary>
        ///     URL    (String )
        /// </summary>
        /// <param name="strName">URL  </param>
        /// <param name="defaultvalue">   </param>
        /// <returns>    URL    </returns>
        public static string GetQueryStringValue(string strName, string defaultvalue)
        {
            if (HttpContext.Current.Request.QueryString[strName] == null || HttpContext.Current.Request.QueryString[strName].ToString() == string.Empty)
                return defaultvalue;
            else
            {
                Regex obj = new Regex("\\w+");
                Match objmach = obj.Match(HttpContext.Current.Request.QueryString[strName].ToString());
                if (objmach.Success)
                    return objmach.Value;
                else
                    return defaultvalue;
            }
        }
		/// <summary>
		///          
		/// </summary>
		/// <returns>       </returns>
		public static string GetPageName()
		{
			string [] urlArr = HttpContext.Current.Request.Url.AbsolutePath.Split('/');
			return urlArr[urlArr.Length - 1].ToLower();
		}

		/// <summary>
		///      Url      
		/// </summary>
		/// <returns></returns>
		public static int GetParamCount()
		{
			return HttpContext.Current.Request.Form.Count + HttpContext.Current.Request.QueryString.Count;
		}

		/// <summary>
		///           
		/// </summary>
		/// <param name="strName">    </param>
		/// <returns>      </returns>
		public static string GetFormString(string strName)
		{
			return GetFormString(strName, false);
		}

        /// <summary>
        ///           
        /// </summary>
        /// <param name="strName">    </param>
        /// <param name="sqlSafeCheck">    SQL    </param>
        /// <returns>      </returns>
        public static string GetFormString(string strName, bool sqlSafeCheck)
        {
            if (HttpContext.Current.Request.Form[strName] == null)
                return "";

            if (sqlSafeCheck && !Utils.IsSafeSqlString(HttpContext.Current.Request.Form[strName]))
                return "unsafe string";

            return HttpContext.Current.Request.Form[strName];
        }
        /// <summary>
        ///           (Int )
        /// </summary>
        /// <param name="strName">    </param>
        /// <returns>          (Int )</returns>
        public static int GetFormIntValue(string strName)
        {
            return GetFormIntValue(strName, 0);
        }
        /// <summary>
        ///           (Int )
        /// </summary>
        /// <param name="strName">    </param>
        /// <param name="defaultvalue">   </param>
        /// <returns>          </returns>
        public static int GetFormIntValue(string strName, int defaultvalue)
        {
            if (HttpContext.Current.Request.Form[strName] == null || HttpContext.Current.Request.Form[strName].ToString() == string.Empty)
                return defaultvalue;
            else
            {
                Regex obj = new Regex("\\d+");
                Match objmach = obj.Match(HttpContext.Current.Request.Form[strName].ToString());
                if (objmach.Success)
                    return Convert.ToInt32(objmach.Value);
                else
                    return defaultvalue;
            }
        }
     /// <summary>
        ///           (String )
     /// </summary>
     /// <param name="strName">    </param>
        /// <returns>          (String )</returns>
        public static string GetFormStringValue(string strName)
        {
            return GetQueryStringValue(strName, string.Empty);
        }
        /// <summary>
        ///           (String )
        /// </summary>
        /// <param name="strName">    </param>
        /// <param name="defaultvalue">   </param>
        /// <returns>          </returns>
        public static string GetFormStringValue(string strName, string defaultvalue)
        {
            if (HttpContext.Current.Request.Form[strName] == null || HttpContext.Current.Request.Form[strName].ToString() == string.Empty)
                return defaultvalue;
            else
            {
                Regex obj = new Regex("\\w+");
                Match objmach = obj.Match(HttpContext.Current.Request.Form[strName].ToString());
                if (objmach.Success)
                    return objmach.Value;
                else
                    return defaultvalue;
            }
        }

		/// <summary>
		///   Url       ,    Url         ,   True         
		/// </summary>
		/// <param name="strName">  </param>
		/// <returns>Url       </returns>
		public static string GetString(string strName)
		{
            return GetString(strName, false);
		}

        /// <summary>
        ///   Url       ,    Url         ,   True         
        /// </summary>
        /// <param name="strName">  </param>
        /// <param name="sqlSafeCheck">    SQL    </param>
        /// <returns>Url       </returns>
        public static string GetString(string strName, bool sqlSafeCheck)
        {
            if ("".Equals(GetQueryString(strName)))
                return GetFormString(strName, sqlSafeCheck);
            else
                return GetQueryString(strName, sqlSafeCheck);
        }
        public static string GetStringValue(string strName)
        {
            return GetStringValue(strName, string.Empty);
        }
        /// <summary>
        ///   Url       ,    Url         ,   True         
        /// </summary>
        /// <param name="strName">  </param>
        /// <param name="sqlSafeCheck">    SQL    </param>
        /// <returns>Url       </returns>
        public static string GetStringValue(string strName, string defaultvalue)
        {
            if ("".Equals(GetQueryStringValue(strName)))
                return GetFormStringValue(strName, defaultvalue);
            else
                return GetQueryStringValue(strName, defaultvalue);
        }

        /// <summary>
        ///     Url   int   
        /// </summary>
        /// <param name="strName">Url  </param>
        /// <returns>Url   int   </returns>
        public static int GetQueryInt(string strName)
        {
            return Utils.StrToInt(HttpContext.Current.Request.QueryString[strName], 0);
        }

		/// <summary>
		///     Url   int   
		/// </summary>
		/// <param name="strName">Url  </param>
		/// <param name="defValue">   </param>
		/// <returns>Url   int   </returns>
		public static int GetQueryInt(string strName, int defValue)
		{
			return Utils.StrToInt(HttpContext.Current.Request.QueryString[strName], defValue);
		}

        /// <summary>
        ///          int   
        /// </summary>
        /// <param name="strName">    </param>
        /// <returns>     int   </returns>
        public static int GetFormInt(string strName)
        {
            return GetFormInt(strName, 0);
        }

		/// <summary>
		///          int   
		/// </summary>
		/// <param name="strName">    </param>
		/// <param name="defValue">   </param>
		/// <returns>     int   </returns>
		public static int GetFormInt(string strName, int defValue)
		{
			return Utils.StrToInt(HttpContext.Current.Request.Form[strName], defValue);
		}

		/// <summary>
		///     Url      int   ,    Url        ,   True         
		/// </summary>
		/// <param name="strName">Url     </param>
		/// <param name="defValue">   </param>
		/// <returns>Url      int   </returns>
		public static int GetInt(string strName, int defValue)
		{
			if (GetQueryInt(strName, defValue) == defValue)
				return GetFormInt(strName, defValue);
			else
				return GetQueryInt(strName, defValue);
		}

        /// <summary>
        ///     Url   decimal   
        /// </summary>
        /// <param name="strName">Url  </param>
        /// <param name="defValue">   </param>
        /// <returns>Url   decimal   </returns>
        public static decimal GetQueryDecimal(string strName, decimal defValue)
        {
            return Utils.StrToDecimal(HttpContext.Current.Request.QueryString[strName], defValue);
        }

        /// <summary>
        ///          decimal   
        /// </summary>
        /// <param name="strName">    </param>
        /// <param name="defValue">   </param>
        /// <returns>     decimal   </returns>
        public static decimal GetFormDecimal(string strName, decimal defValue)
        {
            return Utils.StrToDecimal(HttpContext.Current.Request.Form[strName], defValue);
        }

		/// <summary>
		///     Url   float   
		/// </summary>
		/// <param name="strName">Url  </param>
		/// <param name="defValue">   </param>
		/// <returns>Url   int   </returns>
		public static float GetQueryFloat(string strName, float defValue)
		{
			return Utils.StrToFloat(HttpContext.Current.Request.QueryString[strName], defValue);
		}

		/// <summary>
		///          float   
		/// </summary>
		/// <param name="strName">    </param>
		/// <param name="defValue">   </param>
		/// <returns>     float   </returns>
		public static float GetFormFloat(string strName, float defValue)
		{
			return Utils.StrToFloat(HttpContext.Current.Request.Form[strName], defValue);
		}
		
		/// <summary>
		///     Url      float   ,    Url        ,   True         
		/// </summary>
		/// <param name="strName">Url     </param>
		/// <param name="defValue">   </param>
		/// <returns>Url      int   </returns>
		public static float GetFloat(string strName, float defValue)
		{
			if (GetQueryFloat(strName, defValue) == defValue)
				return GetFormFloat(strName, defValue);
			else
				return GetQueryFloat(strName, defValue);
		}

		

	}
}