상세 컨텐츠

본문 제목

ASP.NET 2.0 - Cleaner approach to handle QueryString

ASP.NET

by happynuri 2008. 5. 28. 11:52

본문

쿼리스트링을 받아와서 사용하기 전에 체크 하는 방식을 서술한 글이기에 퍼왔습니다.



    public static class AppQueryString
    {
        #region Private Constants

        private const string fundID = "fundid";
        private const string fundFlag = "nuflag";

        #endregion

        #region Public Properties

        public static decimal FundID
        {
            get
            {
                decimal result;

                // Check if is null or empty
                if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[fundID]))
                {
                    // Try casting and make sure the data type is correct
                    if (decimal.TryParse(HttpContext.Current.Request.QueryString[fundID], out result))
                    {
                        return result;
                    }
                }
               
                return default(decimal);               
            }
            set
            {
                HttpContext.Current.Request.QueryString[fundID] = value.ToString();//쿼리 스트링을 넣는군요.
            }
        }

        public static bool FundFlag

        {
            get
            {
                byte result;

                // Check if is null or empty
                // Check if the length == 1
                if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString[fundFlag]) && HttpContext.Current.Request.QueryString[fundFlag].Length == 1)
                {
                    // Try casting and make sure the data type is correct
                    if (byte.TryParse(HttpContext.Current.Request.QueryString[fundFlag], out result))
                    {
                        // Check if result == 1
                        if (result == 1)
                        {
                            return true;
                        }
                    }
                }

                return false;
            }
            set
            {
                HttpContext.Current.Request.QueryString[fundFlag] = value.ToString();
            }
        }

        #endregion
    }

관련글 더보기

댓글 영역