过滤掉URL里不需要的参数

复制代码

        /// <summary>
        /// 过滤掉地址里无需要的参数
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="paramname">参数名</param>
        /// <returns></returns>
        public  string FilterUrlParam(string url, string paramname)
        {
            string str = "";
            if (url.ToLower().IndexOf("?") != -1)
            {
                str = url.Substring(url.ToLower().IndexOf("?") + 1);
            }
            else
            {
                return url;
            }
            string[] arr = new string[] { };
            string returnurl = "";
            if (url.ToLower().IndexOf('&') != -1)
            {
                arr = str.Split('&');
                for (int i = 0; i < arr.Length; i++)
                {
                    if (i < arr.Length)
                    {
                        if (arr[i].Split('=')[0].ToLower() != paramname)
                        {
                            returnurl = returnurl + arr[i].Split('=')[0] + "=" + arr[i].Split('=')[1] + "&";
                        }
                    }
                }
                return url.Substring(0, url.IndexOf('?')) + "?" + returnurl.Substring(0, returnurl.Length - 1);
            }
            else
            {
                arr = str.Split('=');
                if (arr[0].ToLower() == paramname)
                {
                    return url.Substring(0, url.IndexOf('?'));
                }
                else
                {
                    return url;
                }
            }
        }
复制代码
posted @ 2015-11-20 17:13  大楚打码人  阅读(721)  评论(0编辑  收藏  举报