![](/img/trans.png)
[英]How can i remove ids one by one from querystring in asp.net using c#?
[英]How can I remove item from querystring in asp.net using c#?
我想從我的 url 中刪除“語言”查詢字符串。我該怎么做? (使用 Asp.net 3.5,c#)
Default.aspx?Agent=10&Language=2
我想刪除“Language=2”,但語言將是第一個、中間或最后一個。 所以我會有這個
Default.aspx?Agent=20
如果它是 HttpRequest.QueryString 則您可以將集合復制到可寫集合中並按照自己的方式進行處理。
NameValueCollection filtered = new NameValueCollection(request.QueryString);
filtered.Remove("Language");
這里有一個簡單的方法。 不需要反射器。
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
集合是只讀的。
最后,
hmemcpy 的回答完全適合我,並感謝其他回答的朋友。
我使用 Reflector 獲取 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 );
我個人的偏好是重寫查詢或在較低的點使用 namevaluecollection,但有時業務邏輯使這兩種方法都沒有太大幫助,有時反射確實是您所需要的。 在這種情況下,您可以像這樣暫時關閉只讀標志:
// 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);
不久前我回答了一個類似的問題。 基本上,最好的方法是使用HttpValueCollection
類, QueryString
屬性實際上就是這個類,不幸的是它在 .NET 框架中是內部的。 您可以使用 Reflector 來抓取它(並將其放入您的 Utils 類中)。 通過這種方式,您可以像 NameValueCollection 一樣操作查詢字符串,但所有 url 編碼/解碼問題都會為您處理。
HttpValueCollection
擴展了NameValueCollection
,並有一個構造函數,它接受一個編碼的查詢字符串(包括與號和問號),它覆蓋了ToString()
方法,以便稍后從底層集合重建查詢字符串。
嘗試這個 ...
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");
HttpContext.Request.QueryString
收集您的查詢字符串。 它默認為NameValueCollection
類型。System.Web.HttpUtility.ParseQueryString()
解析查詢字符串(再次返回NameValueCollection
)。Remove()
函數刪除特定參數(使用鍵引用要刪除的參數)。string.Join()
將查詢字符串格式化為您的 URL 可讀的內容,作為有效的查詢參數。 請參閱下面的工作示例,其中param_to_remove
是您要刪除的參數。
假設您的查詢參數是param1=1¶m_to_remove=stuff¶m2=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¶m2=2
。
您沒有明確說明您是否嘗試修改 Request 對象中的 Querystring。 由於該屬性是只讀的,我想我們會假設您只是想弄亂字符串。
...在這種情況下,這是微不足道的。
獲取querystring集合,解析成( name=value pair
)字符串,不包括要REMOVE的字符串,命名為newQueryString
然后調用Response.Redirect(known_path?newqueryString)
;
如果您已經將查詢字符串作為字符串,您還可以使用簡單的字符串操作:
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);
}
好吧,我有一個簡單的解決方案,但是涉及到一些 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>");
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, "");
您可能想要使用正則表達式來查找要從查詢字符串中刪除的參數,然后將其刪除並將瀏覽器重定向到具有新查詢字符串的同一文件。
是的,.NET 中沒有內置類來編輯查詢字符串。 您必須使用 Regex 或其他一些更改字符串本身的方法。
將 Querystring 解析為 NameValueCollection。 刪除一個項目。 並使用 toString 將其轉換回查詢字符串。
using System.Collections.Specialized;
NameValueCollection filteredQueryString = System.Web.HttpUtility.ParseQueryString(Request.QueryString.ToString());
filteredQueryString.Remove("appKey");
var queryString = '?'+ filteredQueryString.ToString();
在 ASP .NET Core Controller 中,您可以訪問 Request 的實例
Request.Query 是表示查詢參數的查詢集合,將其轉換為列表
您可以從中過濾和刪除所需的參數
使用QueryString.Create,可以將剛剛篩選的列表作為輸入,直接生成查詢字符串
var removeTheseParams = new List<string> {"removeMe1", "removeMe2"}.AsReadOnly(); var filteredQueryParams = Request.Query.ToList().Where(filterKvp =>.removeTheseParams.Contains(filterKvp;Key)). var filteredQueryString = QueryString.Create(queryParamsFilteredList);ToString(): //Example. Console?Writeline(filteredQueryString) will give you "?q1=v1&q2=v2"
下面的可選部分:如果它們不安全,也可以對這些值進行編碼,因此除了上面的 Where() 之外,使用 Select() 對查詢參數鍵和值進行 UrlEncode 編碼,如下所示:
//Optional
.Select(cleanKvp => new KeyValuePair<string, string?>(UrlEncoder.Default.Encode(cleanKvp.Key),UrlEncoder.Default.Encode(cleanKvp.Value)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.