[英]How do I remove items from the query string for redirection?
In my base page I need to remove an item from the query string and redirect. 在我的基页中,我需要从查询字符串中删除一个项目并重定向。 I can't use
我不能用
Request.QueryString.Remove("foo")
because the collection is read-only. 因为该集合是只读的。 Is there any way to get the query string (except for that one item) without iterating through the collection and re-building it?
有没有办法获取查询字符串(除了那一项)而不迭代整个集合并重新构建它?
You can avoid touching the original query string by working on a copy of it instead. 您可以通过处理原始查询字符串来避免触及原始查询字符串。 You can then redirect the page to the a url containing your modified query string like so:
然后,您可以将页面重定向到包含修改后的查询字符串的URL,如下所示:
var nvc = new NameValueCollection();
nvc.Add(HttpUtility.ParseQueryString(Request.Url.Query));
nvc.Remove("foo");
string url = Request.Url.AbsolutePath;
for (int i = 0; i < nvc.Count; i++)
url += string.Format("{0}{1}={2}", (i == 0 ? "?" : "&"), nvc.Keys[i], nvc[i]);
Response.Redirect(url);
Update: 更新:
Turns out we can simplify the code like so: 事实证明我们可以像这样简化代码:
var nvc = HttpUtility.ParseQueryString(Request.Url.Query);
nvc.Remove("foo");
string url = Request.Url.AbsolutePath + "?" + nvc.ToString();
Response.Redirect(url);
You'd have to reconstruct the url and then redirect. 您必须重建网址然后重定向。 Something like this:
像这样的东西:
string url = Request.RawUrl;
NameValueCollection params = Request.QueryString;
for (int i=0; i<params.Count; i++)
{
if (params[i].GetKey(i).ToLower() == "foo")
{
url += string.Concat((i==0 ? "?" : "&"), params[i].GetKey(i), "=", params.Get(i));
}
}
Response.Redirect(url);
Anyway, I didn't test that or anything, but it should work (or at least get you in thye right direction) 无论如何,我没有测试那个或任何东西,但它应该工作(或至少让你在正确的方向)
Response.Redirect(String.Format("nextpage.aspx?{0}", Request.QueryString.ToString().Replace("foo", "mangledfoo")));
I quick hack, saves you little. 我快速破解,拯救你一点点。 But foo will not be present for the code awaiting it in nextpge.aspx :)
但是foo将不会出现在nextpge.aspx中等待它的代码:)
Interesting question. 有趣的问题。 I don't see any real viable alternative to manually copying the collection since CopyTo will only allow you to get the values (and not the keys).
我没有看到任何真正可行的替代手动复制集合,因为CopyTo只允许你获取值(而不是键)。
I think HollyStyles' Hack would work (although I would be nervous about putting a Replace in a QueryString - obv. dependant on use case), but there is one thing thats bothering me.. 我认为HollyStyles的Hack会起作用(虽然我会担心将一个替换放在一个QueryString中 - 依赖于用例),但有一件事困扰着我。
If the target page is not reading it, why do you need to remove it from the QueryString? 如果目标页面没有读取它,为什么需要从QueryString中删除它?
It will just be ignored? 它会被忽略吗?
Failing that, I think you would just need to bite the bullet and create a util method to alter the collection for you. 如果做不到这一点,我认为你只需要咬紧牙关并创建一个util方法来为你改变收藏。
Ahhhh! 哈啊! I see now, yes, I have had similar problems with SiteMap performing full comparison of the string.
我现在看到了,是的,我遇到了与SiteMap进行完全比较的类似问题。
Since changing the other source code (ie the search) is out of the question, I would probably say it may be best to do a Replace on the string. 由于更改其他源代码(即搜索)是不可能的,我可能会说最好对字符串执行替换。 Although to be fair, if you often encounter code similar to this, it would equally be just as quick to set up a utility function to clone the collection, taking an array of values to filter from it.
虽然公平地说,如果您经常遇到与此类似的代码,那么设置实用程序函数以克隆集合同样快速,从中过滤一系列值。
This way you would never have to worry about such issues again :) 这样你再也不用担心这样的问题了:)
HttpUtility.ParseQueryString(Request.Url.Query)
return is QueryStringValueCollection
. HttpUtility.ParseQueryString(Request.Url.Query)
返回的是QueryStringValueCollection
。 It is inherit from NameValueCollection
. 它继承自
NameValueCollection
。
var qs = HttpUtility.ParseQueryString(Request.Url.Query);
qs.Remove("foo");
string url = "~/Default.aspx";
if (qs.Count > 0)
url = url + "?" + qs.ToString();
Response.Redirect(url);
I found this was a more elegant solution 我发现这是一个更优雅的解决方案
var qs = HttpUtility.ParseQueryString(Request.QueryString.ToString());
qs.Remove("item");
Console.WriteLine(qs.ToString());
Here is a solution that uses LINQ against the Request.QueryString
which allows for complex filtering of qs params if required. 这是一个针对
Request.QueryString
使用LINQ的解决方案,它允许在需要时对qs参数进行复杂的过滤。
The example below shows me filtering out the uid
param to create a relative url ready for redirection. 下面的示例显示我过滤掉
uid
参数以创建准备重定向的相对URL。
Before: http://www.domain.com/home.aspx?color=red&uid=1&name=bob
之前:
http://www.domain.com/home.aspx?color=red&uid=1&name=bob
: http://www.domain.com/home.aspx?color=red&uid=1&name=bob
red& http://www.domain.com/home.aspx?color=red&uid=1&name=bob
name = http://www.domain.com/home.aspx?color=red&uid=1&name=bob
After: ~/home.aspx?color=red&name=bob
之后:
~/home.aspx?color=red&name=bob
Remove QS param from url 从网址中删除QS参数
var rq = this.Request.QueryString;
var qs = string.Join("&", rq.Cast<string>().Where(k => k != "uid").Select(k => string.Format("{0}={1}", k, rq[k])).ToArray());
var url = string.Concat("~", Request.RawUrl.Split('?')[0], "?", qs);
The search page appends "&terms=" to the query string for highlighting, so it messes it up.
搜索页面将“&terms =”附加到查询字符串以突出显示,因此将其混乱。
Only other option is a regex replace. 只有其他选项是正则表达式替换。 If you know for sure that &terms is in the middle of the collection somewhere leave the trailing & in the regex, if you know for sure it's on the end then drop the trailing & and change the replacement string "&" to String.Empty
如果你肯定知道&terms是在集合的中间某处留下尾随&在正则表达式中,如果你确定它在最后然后删除尾随&并将替换字符串“&”更改为String.Empty
Response.Redirect(String.Format("nextpage.aspx?{0}", Regex.Replace(Request.QueryString.ToString(), "&terms=.*&", "&"));
Request.Url.GetLeftPart(UriPartial.Path)
应该这样做
Can you clone the collection and then redirect to the page with the cloned (and modified) collection? 您可以克隆该集合,然后重定向到包含克隆(和修改)集合的页面吗?
I know it's not much better than iterating... 我知道它比迭代好多了......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.