如何使用 c# 从 asp.net 中的查询字符串中删除项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/529551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i remove item from querystring in asp.net using c#?
提问by Barbaros Alp
I want remove "Language" querystring from my url. How can i do this ? (using Asp.net 3.5 , c#)
我想从我的网址中删除“语言”查询字符串。我怎样才能做到这一点 ?(使用 Asp.net 3.5,c#)
Default.aspx?Agent=10&Language=2
I want to remove "Language=2", but language would be the first,middle or last. so i will have this
我想删除“语言= 2”,但语言将是第一个、中间或最后一个。所以我要这个
Default.aspx?Agent=20
采纳答案by Igal Tabachnik
I answered a similar questiona while ago. Basically, the best way would be to use the class HttpValueCollection
, which the QueryString
property actually is, unfortunately it is internal in the .NET framework.
You could use Reflector to grab it (and place it into your Utils class). This way you could manipulate the query string like a NameValueCollection, but with all the url encoding/decoding issues taken care for you.
不久前我回答了一个类似的问题。基本上,最好的方法是使用 class HttpValueCollection
,它QueryString
实际上是属性,不幸的是它在 .NET 框架中是内部的。您可以使用 Reflector 来抓取它(并将其放入您的 Utils 类中)。通过这种方式,您可以像 NameValueCollection 一样操作查询字符串,但所有 url 编码/解码问题都会为您处理。
HttpValueCollection
extends NameValueCollection
, and has a constructor that takes an encoded query string (ampersands and question marks included), and it overrides a ToString()
method to later rebuild the query string from the underlying collection.
HttpValueCollection
extends NameValueCollection
,并有一个构造函数,它接受一个编码的查询字符串(包括&符号和问号),它覆盖了一个ToString()
方法,以便稍后从基础集合中重建查询字符串。
回答by RKitson
You're probably going to want use a Regular Expression to find the parameter you want to remove from the querystring, then remove it and redirect the browser to the same file with your new querystring.
您可能想要使用正则表达式来查找要从查询字符串中删除的参数,然后将其删除并将浏览器重定向到具有新查询字符串的同一文件。
回答by David Morton
Yes, there are no classes built into .NET to edit query strings. You'll have to either use Regex or some other method of altering the string itself.
是的,.NET 中没有内置类来编辑查询字符串。您必须使用 Regex 或其他一些更改字符串本身的方法。
回答by Jason Kester
You don't make it clear whether you're trying to modify the Querystring in place in the Request object. Since that property is read-only, I guess we'll assume you just want to mess with the string.
您没有明确说明您是否尝试修改 Request 对象中的 Querystring。由于该属性是只读的,我想我们会假设您只是想弄乱字符串。
... In which case, it's borderline trivial.
...在这种情况下,这是微不足道的。
- grab the querystring off the Request
- .split() it on '&'
- put it back together into a new string, while sniffing for and tossing out anything starting with "language"
- 从请求中获取查询字符串
- .split() 它在 '&'
- 将它重新组合成一个新的字符串,同时嗅探并丢弃以“语言”开头的任何内容
回答by Pita.O
Get the querystring collection, parse it into a (name=value pair
) string, excluding the one you want to REMOVE, and name it newQueryString
获取querystring集合,解析为( name=value pair
)字符串,不包括要REMOVE的字符串,命名为newQueryString
Then call Response.Redirect(known_path?newqueryString)
;
然后打电话Response.Redirect(known_path?newqueryString)
;
回答by xcud
If it's the HttpRequest.QueryString then you can copy the collection into a writable collection and have your way with it.
如果它是 HttpRequest.QueryString 则您可以将集合复制到可写集合中并按照自己的方式进行处理。
NameValueCollection filtered = new NameValueCollection(request.QueryString);
filtered.Remove("Language");
回答by Barbaros Alp
Finally,
最后,
hmemcpy answer was totally for me and thanks to other friends who answered.
hmemcpy 的回答完全适合我,并感谢其他回答的朋友。
I grab the HttpValueCollection using Reflector and wrote the following code
我使用 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 );
回答by annakata
My personal preference here is rewriting the query or working with a namevaluecollection at a lower point, but there are times where the business logic makes neither of those very helpful and sometimes reflection really is what you need. In those circumstances you can just turn off the readonly flag for a moment like so:
我个人的偏好是重写查询或在较低的点使用 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);
回答by Paulius Zaliaduonis
Here is a simple way. Reflector is not needed.
这里有一个简单的方法。不需要反射器。
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;
}
Here QueryString.ToString()
is required because Request.QueryString
collection is read only.
这QueryString.ToString()
是必需的,因为Request.QueryString
集合是只读的。
回答by Pedro Geada
If you have already the Query String as a string, you can also use simple string manipulation:
如果您已经将查询字符串作为字符串,您还可以使用简单的字符串操作:
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);
}