2012-05-24 71 views
0

說我目前的頁面url已經得到(http://mysite/english/faq.aspx?faqid = 12123 & cid = 4545 & intcid = 65456 & H =人)如何從c#2.0中刪除當前網頁查詢字符串的查詢字符串的特殊列表

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; 
     } 

回答

1

方式下面是完美的解決方案我得到了它,在這個任何評論。

string excludeList = "cid,intcid,del"; 

string getFinalString = Regex.Replace(Regex.Replace(Regex.Replace(Request.Url.Query, @"^\?", "&"), "&(" + excludeList.Replace(",", "|") + ")=[^&]*", "", RegexOptions.IgnoreCase), "^&", "?"); 
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"); 

希望這將幫助你!

+0

我不會刪除直接查詢字符串,首先我會檢查當前頁面的網址是否已經有任何查詢字符串,如果是我會檢查查詢字符串中是否包含我的排除列表中,如果任何查詢字符串拿到賽我的名單,然後我將刪除從我的整個查詢字符串列表中,進一步我會將該查詢字符串傳遞給我的xslt進一步用法,有關這方面的任何想法我們如何能做到這一點 –

+0

它的好,現在你只要把條件如你所願(/ /你的條件)這樣...沒有人爲你寫代碼。試試你的東西! – Ahsan

+0

是否有任何微軟內置功能,我們可以匹配一個數組與其他沒有兩個循環 –

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 
+0

嗨,Ahsan,我改變了上面的代碼見編輯部分,一切工作完美,但再次卡住,而我要轉回到querystring ...任何想法或解決方案,我使用的是C#2.0 –