Bar*_*Alp 54 .net c# asp.net query-string
我想从我的网址中删除"语言"查询字符串.我怎样才能做到这一点 ?(使用Asp.net 3.5,c#)
Default.aspx?Agent=10&Language=2
Run Code Online (Sandbox Code Playgroud)
我想删除"语言= 2",但语言将是第一个,中间或最后一个.所以我会有这个
Default.aspx?Agent=20
Run Code Online (Sandbox Code Playgroud)
xcu*_*cud 115
如果它是HttpRequest.QueryString,那么您可以将集合复制到可写集合中并使用它.
NameValueCollection filtered = new NameValueCollection(request.QueryString);
filtered.Remove("Language");
Run Code Online (Sandbox Code Playgroud)
Pau*_*nis 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;
}
Run Code Online (Sandbox Code Playgroud)
这QueryString.ToString()是必需的,因为Request.QueryString集合是只读的.
Bar*_*Alp 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 );
Run Code Online (Sandbox Code Playgroud)
ann*_*ata 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);
Run Code Online (Sandbox Code Playgroud)
Iga*_*nik 10
我刚才回答了类似的问题.基本上,最好的办法是使用类HttpValueCollection,它的QueryString属性实际上,不幸的是它是在.NET框架内.您可以使用Reflector来抓取它(并将其放入Utils类中).这样您就可以像NameValueCollection那样操纵查询字符串,但是所有的url编码/解码问题都会得到照顾.
HttpValueCollectionextends NameValueCollection,并且有一个构造函数,它接受一个编码的查询字符串(包括&符号和问号),并覆盖一个ToString()方法,以便稍后从底层集合重建查询字符串.
| 归档时间: |
|
| 查看次数: |
108043 次 |
| 最近记录: |