![](/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.