0

假设我当前的页面 url 有 (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;
        }
4

3 回答 3

1

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

string excludeList = "cid,intcid,del";

string getFinalString = Regex.Replace(Regex.Replace(Regex.Replace(Request.Url.Query, @"^\?", "&"), "&(" + excludeList.Replace(",", "|") + ")=[^&]*", "", RegexOptions.IgnoreCase), "^&", "?");
于 2012-05-25T09:17:44.260 回答
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");

希望对你有帮助 !!

于 2012-05-24T10:02:54.303 回答
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
于 2012-05-25T06:49:03.997 回答