54

我想从我的网址中删除“语言”查询字符串。我怎样才能做到这一点?(使用 Asp.net 3.5,c#)

Default.aspx?Agent=10&Language=2

我想删除“Language=2”,但语言将是第一个、中间或最后一个。所以我会有这个

Default.aspx?Agent=20
4

15 回答 15

115

如果它是 HttpRequest.QueryString ,那么您可以将集合复制到可写集合中并随心所欲地使用它。

NameValueCollection filtered = new NameValueCollection(request.QueryString);
filtered.Remove("Language");
于 2009-02-09T19:51:36.950 回答
46

这是一个简单的方法。不需要反射器。

    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集合是只读的。

于 2011-09-23T14:11:36.637 回答
36

最后,

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 );
于 2009-02-09T21:35:24.360 回答
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);
于 2009-10-08T08:45:16.580 回答
10

不久前我回答了一个类似的问题。基本上,最好的方法是使用HttpValueCollectionQueryString属性实际上是的 class ,不幸的是它在 .NET 框架中是内部的。您可以使用 Reflector 来抓取它(并将其放入您的 Utils 类中)。通过这种方式,您可以像 NameValueCollection 一样操作查询字符串,但所有 url 编码/解码问题都会为您处理好。

HttpValueCollectionextends NameValueCollection,并具有一个构造函数,该构造函数采用编码的查询字符串(包括与号和问号),并且它覆盖了一个ToString()稍后从基础集合重建查询字符串的方法。

于 2009-02-09T19:59:44.027 回答
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");
于 2013-06-11T17:31:36.933 回答
1

您不清楚是否尝试修改 Request 对象中的 Querystring。由于该属性是只读的,我想我们会假设您只想弄乱字符串。

...在这种情况下,这是微不足道的。

  • 从请求中获取查询字符串
  • .split() 它在 '&' 上
  • 将其重新组合成一个新字符串,同时嗅探并丢弃以“语言”开头的任何内容
于 2009-02-09T19:44:57.573 回答
1

获取查询字符串集合,将其解析为 ( name=value pair) 字符串,不包括要删除的字符串,并将其命名为 newQueryString

然后调用Response.Redirect(known_path?newqueryString)

于 2009-02-09T19:45:28.197 回答
1
  1. 使用 收集您的查询字符串HttpContext.Request.QueryString。它默认为一种NameValueCollection类型。
  2. 将其转换为字符串并用于System.Web.HttpUtility.ParseQueryString()解析查询字符串(NameValueCollection再次返回 a)。
  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.

于 2016-02-29T23:33:07.190 回答
0

您可能需要使用正则表达式来查找要从查询字符串中删除的参数,然后将其删除并将浏览器重定向到具有新查询字符串的同一文件。

于 2009-02-09T19:42:38.250 回答
0

是的,.NET 中没有内置类来编辑查询字符串。您必须使用正则表达式或其他更改字符串本身的方法。

于 2009-02-09T19:42:48.500 回答
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);
}
于 2012-11-27T15:20:58.410 回答
0

好吧,我有一个简单的解决方案,但是涉及到一点 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>");
于 2015-02-03T09:06:13.860 回答
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

于 2017-12-20T13:05:16.780 回答
0

将 Querystring 解析为 NameValueCollection。删除一个项目。并使用 toString 将其转换回查询字符串。

using System.Collections.Specialized;

NameValueCollection filteredQueryString = System.Web.HttpUtility.ParseQueryString(Request.QueryString.ToString());
filteredQueryString.Remove("appKey");

var queryString = '?'+ filteredQueryString.ToString();
于 2019-04-30T04:29:22.363 回答