URL から「Language」クエリ文字列を削除したい。これどうやってするの?(Asp.net 3.5、c# を使用)
Default.aspx?Agent=10&Language=2
「Language=2」を削除したいのですが、language が最初、中間、または最後になります。だから私はこれを持っています
Default.aspx?Agent=20
URL から「Language」クエリ文字列を削除したい。これどうやってするの?(Asp.net 3.5、c# を使用)
Default.aspx?Agent=10&Language=2
「Language=2」を削除したいのですが、language が最初、中間、または最後になります。だから私はこれを持っています
Default.aspx?Agent=20
それが HttpRequest.QueryString の場合、コレクションを書き込み可能なコレクションにコピーして、好きなように処理できます。
NameValueCollection filtered = new NameValueCollection(request.QueryString);
filtered.Remove("Language");
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.
ついに、
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);
これを試して ...
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 を変更しようとしているかどうかを明確にしません。そのプロパティは読み取り専用なので、文字列をいじりたいだけだと思います。
…という場合は、些細なことです。
querystring コレクションを取得し、それを ( name=value pair
) 文字列に解析して、削除するものを除外し、newQueryString という名前を付けます。
次に呼び出しResponse.Redirect(known_path?newqueryString)
ます。
おそらく、正規表現を使用してクエリ文字列から削除するパラメーターを見つけ、それを削除して、ブラウザーを新しいクエリ文字列と同じファイルにリダイレクトする必要があります。
はい、クエリ文字列を編集するために .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);
}