清除或删除ASP.Net中的查询字符串 [英] Clear or Remove query string in ASP.Net

查看:97
本文介绍了清除或删除ASP.Net中的查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为'flagEdit'的 QueryString ,我想在获取其值后将其删除.但是当我尝试使用

I have a QueryString named 'flagEdit' and I want to remove it after fetching it's value. But when i try to remove it using

Request.QueryString.Clear();

Request.QueryString.Remove("editFlag");

发生此错误-

System.NotSupportedException:集合是只读的.

System.NotSupportedException: Collection is read-only.

所以,我想知道如何在获取值后删除查询字符串

So, I want to know how to remove query string after fetches it's value

推荐答案

在ASP.NET中删除(删除)查询字符串

Request.QueryString.Remove("editFlag")

如果执行上述操作,将会收到错误消息

If you do the above, you will get an error

集合是只读的.

因此,我们需要在删除查询字符串之前编写以下代码.

So, we need to write the below code before deleting the query string.

尝试这种方式

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("editFlag");

您也可以尝试这种方式

    var nvc = HttpUtility.ParseQueryString(Request.Url.Query);
    nvc.Remove("editFlag");
    string url = Request.Url.AbsolutePath + "?" + nvc.ToString();
     Response.Redirect(url);

希望这会有所帮助

这篇关于清除或删除ASP.Net中的查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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