2009-02-09 127 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