2009-02-09 123 views
51

我想從我的網址中刪除「語言」查詢字符串。我怎樣才能做到這一點 ? (使用Asp.net 3.5,C#)如何從c#中的asp.net中的querystring中刪除項目?

Default.aspx?Agent=10&Language=2 

我想刪除「語言= 2」,但語言將是第一個,中間或最後一個。所以我纔會有這種

Default.aspx?Agent=20 
+0

請解釋更多的你的目的是什麼。 我不認爲你正在尋找一種方式來編輯用戶地址字段,是嗎? – 2009-02-09 19:56:23

回答

10

我剛纔回答了similar question。基本上,最好的方法是使用類HttpValueCollection,這實際上屬於QueryString屬性,不幸的是它在.NET框架中是內部的。 您可以使用Reflector來抓取它(並將其放入您的Utils類中)。這樣你可以像查詢NameValueCollection一樣操縱查詢字符串,但是所有的url編碼/解碼問題都會照顧你。

HttpValueCollection延伸NameValueCollection,並且具有一個構造函數編碼的查詢字符串(&符號和問號包括在內),和它覆蓋了一個ToString()方法後來重建從底層集合的查詢字符串。

+0

我已閱讀你的文章,聽起來不錯。感謝您的回答,我會嘗試並回復 – 2009-02-09 20:28:13

0

你可能會想使用正則表達式來找到你想從查詢字符串中刪除,然後將其刪除,並用新的查詢字符串瀏覽器重定向到同一個文件中的參數。

0

是的,.NET中沒有內置編輯查詢字符串的類。你必須使用正則表達式或其他一些方法來改變字符串本身。

1

你不清楚你是否試圖在請求對象中修改Querystring。由於該屬性是隻讀的,我想我們會假設你只是想弄亂字符串。

...在這種情況下,它是邊界微不足道的。

  • 搶)查詢字符串關閉請求
  • .split(它「&」
  • 把它重新走到一起成爲一個新的字符串,而嗅探和折騰出什麼開始的「語言」
+0

同意,如果你不想亂用RegEx的,從Rquest.Url.Query獲取查詢,拆分&,並重建它,而不使用語言查詢字符串。您可以使用UriBuilder,但它沒有考慮名稱值對構建查詢字符串:( – 2009-02-09 20:16:10

+0

我早就建立了一個可以撕開的網址我自己LinkUri類,建立它們趕走的鍵/值,和的ToString()他們入網址一次。基本上,它確實你的願望UriBuilder一樣。一上午花了4年前放在一起,我用它每一天。 – 2009-02-09 20:26:21

+0

我就可以知道了,我討厭用繩子,喔:)神但隨着打這樣,我覺得最舒服 – 2009-02-09 20:27:39

1

獲取查詢字符串集合,它解析爲(name=value pair)字符串,不包括你要刪除的一個,並將其命名爲newQueryString

然後致電Response.Redirect(known_path?newqueryString);

+0

這是我嘗試過的第一件事,謝謝。 – 2009-02-09 19:58:27

113

如果是HttpRequest.QueryString,那麼你可以將集合複製到一個可寫的集合中,並按照它的方式。

NameValueCollection filtered = new NameValueCollection(request.QueryString); 
filtered.Remove("Language"); 
+2

這是既簡單又優雅 - 希望我可以碰到幾次 – Doug 2011-02-07 06:33:07

+5

感謝道格。我對接受的答案感到有點困惑。這聽起來像是提問者正在導致另一個導航出現,以從查詢字符串中獲取不需要的參數。 – xcud 2011-02-07 20:24:49

+0

System.Collections.Specialized.NameValueCollection filtered = new System.Collections.Specialized.NameValueCollection(Request.QueryString); – 2016-03-17 17:16:05

36

最後,

hmemcpy答案是完全爲我,並感謝其他朋友誰回答。

我使用反射搶HttpValueCollection並且寫了下面的代碼

 var hebe = new HttpValueCollection(); 
     hebe.Add(HttpUtility.ParseQueryString(Request.Url.Query)); 

     if (!string.IsNullOrEmpty(hebe["Language"])) 
      hebe.Remove("Language"); 

     Response.Redirect(Request.Url.AbsolutePath + "?" + hebe); 
26

這裏我個人的偏好重寫查詢或在一個較低的點的NameValueCollection工作,但有時其中的業務邏輯,使既不那些非常有幫助,有時反思真的是你需要的。在這種情況下,您可以關閉readonly標誌片刻:

// reflect to readonly property 
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"); 

// modify 
this.Request.QueryString.Set("bar", "123"); 

// make collection readonly again 
isreadonly.SetValue(this.Request.QueryString, true, null); 
44

這是一種簡單的方法。反射器是不需要的。

public static string GetQueryStringWithOutParameter(string parameter) 
    { 
     var nameValueCollection = System.Web.HttpUtility.ParseQueryString(HttpContext.Current.Request.QueryString.ToString()); 
     nameValueCollection.Remove(parameter); 
     string url = HttpContext.Current.Request.Path + "?" + nameValueCollection; 

     return url; 
    } 

這裏QueryString.ToString()是必需的,因爲Request.QueryString集合爲只讀。

0

如果您已經將查詢字符串作爲一個字符串,也可以用簡單的字符串操作:

int pos = queryString.ToLower().IndexOf("parameter="); 
if (pos >= 0) 
{ 
    int pos_end = queryString.IndexOf("&", pos); 
    if (pos_end >= 0) // there are additional parameters after this one 
     queryString = queryString.Substring(0, pos) + queryString.Substring(pos_end + 1); 
    else 
     if (pos == 0) // this one is the only parameter 
      queryString = ""; 
     else  // this one is the last parameter 
      queryString=queryString.Substring(0, pos - 1); 
} 
3

嘗試......

PropertyInfo isreadonly =typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);  

isreadonly.SetValue(this.Request.QueryString, false, null); 
this.Request.QueryString.Remove("foo"); 
-1

好,我有一個簡單的解決方案,但有一點JavaScript涉及。

假設查詢字符串 「OK = 1」

string url = Request.Url.AbsoluteUri.Replace("&ok=1", ""); 
    url = Request.Url.AbsoluteUri.Replace("?ok=1", ""); 
    Response.Write("<script>window.location = '"+url+"';</script>"); 
1
  1. 使用HttpContext.Request.QueryString收集您的查詢字符串。它默認爲NameValueCollection類型。
  2. 將其轉換爲字符串並使用System.Web.HttpUtility.ParseQueryString()解析查詢字符串(它將再次返回NameValueCollection)。
  3. 然後,您可以使用Remove()函數刪除特定參數(使用鍵來引用該參數以刪除)。
  4. 用例查詢參數返回到字符串,並使用string.Join()將查詢字符串格式化爲URL可讀的內容,作爲有效的查詢參數。

查看下面的工作示例,其中param_to_remove是您要刪除的參數。

假設您的查詢參數是param1=1&param_to_remove=stuff&param2=2。運行以下行:

var queryParams = System.Web.HttpUtility.ParseQueryString(HttpContext.Request.QueryString.ToString()); 
queryParams.Remove("param_to_remove"); 
string queryString = string.Join("&", queryParams.Cast<string>().Select(e => e + "=" + queryParams[e])); 

現在您的查詢字符串應該是param1=1&param2=2

0
string queryString = "Default.aspx?Agent=10&Language=2"; //Request.QueryString.ToString(); 
string parameterToRemove="Language"; //parameter which we want to remove 
string regex=string.Format("(&{0}=[^&\s]+|{0}=[^&\s]+&?)",parameterToRemove); 
string finalQS = Regex.Replace(queryString, regex, ""); 

https://regexr.com/3i9vj