如何从c#2.0中的当前页面url querystring中删除querystring的partucular列表

时间:2012-05-24 09:46:06

标签: c#-2.0 request.querystring

说我当前的网页网址已经有了(http://mysite/english/faq.aspx?faqid = 12123& cid = 4545& intcid = 65456& h = man)

string excludeQuerystring = DynConfig.Item("GoogleSEOLinkSettings/ExcludeQuerystring"); //this is the list of my exclude querystring (cid,intcid,del)

querystring = HttpContext.Current.Request.Url.AbsoluteUri.Split('?')[1]; //I will get faqid=12123&cid=4545,intcid=65456

StringBuilder fullQueryString = new StringBuilder();

if (!string.IsNullOrEmpty(excludeQuerystring) && !string.IsNullOrEmpty(querystring))
{
    string[] strEQ = excludeQuerystring.Split(',');  //making a array of excluded querystrings

    NameValueCollection navValues = HttpUtility.ParseQueryString(querystring);  //getting the list of querystring in NameValueCollection

    if (navValues.Count > 0)
    {
        string[] strQ = navValues.AllKeys;
        if(strQ.Length>0)
        {

        }
    }
}   

querystring= ?+faqid=12123&h=man //here I want updated querystring which does not have any querystring which is there in my excludeQuerystring

我很困惑如何得到这个,实际上我想制作一个能够完成这一切的功能。

请建议!!

编辑:

我应用新代码来解决上述问题,但是在将NameValueCollection再次转换为查询字符串时几乎没有陷入困境。

 protected void Page_Load(object sender, EventArgs e)
        {
            string querystring = string.Empty;

            string excludeList = "cid,intcid,del";

            if (!string.IsNullOrEmpty(excludeList))
            {
                string getFinalString = GetQueryString(excludeList);
                getFinalString = "?" + getFinalString;
            }
        }

        public string GetQueryString(string excludeArray)
        {
            string retQueryString = string.Empty;
            if (excludeArray.IndexOf(",") != -1)
            {
                string[] strArray = excludeArray.Split(",".ToCharArray());
                NameValueCollection filtered = new NameValueCollection();

                filtered.Add(HttpUtility.ParseQueryString(Request.Url.Query));
                if (filtered.HasKeys())
                {
                    foreach (string strMatch in strArray)
                    {
                        filtered.Remove(strMatch);
                    }
                    retQueryString = filtered.ToString(); //Here I am not able to convert back to querystring, however there are other ways to get it like (http://leekelleher.com/2008/06/06/how-to-convert-namevaluecollection-to-a-query-string/), is there any other way to do that
                }
            }

            return retQueryString;
        }

3 个答案:

答案 0 :(得分:1)

以下是我得到的完美解决方案,对此有任何意见。

string excludeList = "cid,intcid,del";

string getFinalString = Regex.Replace(Regex.Replace(Regex.Replace(Request.Url.Query, @"^\?", "&"), "&(" + excludeList.Replace(",", "|") + ")=[^&]*", "", RegexOptions.IgnoreCase), "^&", "?");

答案 1 :(得分:0)

我们无法直接删除查询字符串,如下所示:

Request.QueryString.Remove("foo")

如果这样做,您将收到错误 - 收集是只读的。因此,我们需要在删除查询字符串之前编写以下代码。

在C#中:

PropertyInfo isreadonly = 
typeof(System.Collections.Specialized.NameValueCollection).GetProperty(
 "IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
 // make collection editable
 isreadonly.SetValue(this.Request.QueryString, false, null);
 // remove
 this.Request.QueryString.Remove("foo");

希望这会对你有所帮助!!

答案 2 :(得分:0)

是的,有一种比较两个数组的方法

        var array1 = new byte[] { 1, 2, 5, 4 };
        var array2 = new byte[] { 1, 2, 3, 4 };

        var areEqual = array1.SequenceEqual(array2); //return boolean value True or False