54

URL から「Language」クエリ文字列を削除したい。これどうやってするの?(Asp.net 3.5、c# を使用)

Default.aspx?Agent=10&Language=2

「Language=2」を削除したいのですが、language が最初、中間、または最後になります。だから私はこれを持っています

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

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.

于 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 を操作することですが、ビジネス ロジックではどちらもあまり役に立たない場合があり、リフレクションが本当に必要な場合もあります。そのような状況では、次のように読み取り専用フラグを一時的にオフにすることができます。

// 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 に答える
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

querystring コレクションを取得し、それを ( name=value pair) 文字列に解析して、削除するものを除外し、newQueryString という名前を付けます。

次に呼び出しResponse.Redirect(known_path?newqueryString)ます。

于 2009-02-09T19:45:28.197 に答える
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 に答える