我想从我的网址中删除“语言”查询字符串。我怎样才能做到这一点?(使用 Asp.net 3.5,c#)
Default.aspx?Agent=10&Language=2
我想删除“Language=2”,但语言将是第一个、中间或最后一个。所以我会有这个
Default.aspx?Agent=20
我想从我的网址中删除“语言”查询字符串。我怎样才能做到这一点?(使用 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,但有时业务逻辑不会使这些都非常有用,有时反射确实是您需要的。在这种情况下,您可以像这样关闭 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);
不久前我回答了一个类似的问题。基本上,最好的方法是使用HttpValueCollection
该QueryString
属性实际上是的 class ,不幸的是它在 .NET 框架中是内部的。您可以使用 Reflector 来抓取它(并将其放入您的 Utils 类中)。通过这种方式,您可以像 NameValueCollection 一样操作查询字符串,但所有 url 编码/解码问题都会为您处理好。
HttpValueCollection
extends 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");
您不清楚是否尝试修改 Request 对象中的 Querystring。由于该属性是只读的,我想我们会假设您只想弄乱字符串。
...在这种情况下,这是微不足道的。
获取查询字符串集合,将其解析为 ( name=value pair
) 字符串,不包括要删除的字符串,并将其命名为 newQueryString
然后调用Response.Redirect(known_path?newqueryString)
;
HttpContext.Request.QueryString
。它默认为一种NameValueCollection
类型。System.Web.HttpUtility.ParseQueryString()
解析查询字符串(NameValueCollection
再次返回 a)。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
.
您可能需要使用正则表达式来查找要从查询字符串中删除的参数,然后将其删除并将浏览器重定向到具有新查询字符串的同一文件。
是的,.NET 中没有内置类来编辑查询字符串。您必须使用正则表达式或其他更改字符串本身的方法。
如果您已经将查询字符串作为字符串,您还可以使用简单的字符串操作:
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, "");
将 Querystring 解析为 NameValueCollection。删除一个项目。并使用 toString 将其转换回查询字符串。
using System.Collections.Specialized;
NameValueCollection filteredQueryString = System.Web.HttpUtility.ParseQueryString(Request.QueryString.ToString());
filteredQueryString.Remove("appKey");
var queryString = '?'+ filteredQueryString.ToString();