如何删除从查询字符串重定向项目? [英] How do I remove items from the query string for redirection?

查看:183
本文介绍了如何删除从查询字符串重定向项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的基地页,我需要从查询字符串中删除项目和重定向。我不能用

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?

推荐答案

您不得不重建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);

反正我没有测试或任何东西,但它应该工作(或者至少让你在THYE正确的方向)

Anyway, I didn't test that or anything, but it should work (or at least get you in thye right direction)

这篇关于如何删除从查询字符串重定向项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆