在 C# 中输出操纵的 QueryString

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/229925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:05:04  来源:igfitidea点击:

Outputting a manipulated QueryString in C#

c#asp.netcollectionsquery-string

提问by Anthony Main

Using the following code I get a nice formatted string:

使用以下代码,我得到了一个很好的格式化字符串:

Request.QueryString.ToString 

Gives me something like: &hello=world&microsoft=sucks

给了我类似的东西:&hello=worldµsoft=sucks

But when I use this code to clone the collection to another object (of the same type) I get the Type() back from the ToString() method instead.

但是,当我使用此代码将集合克隆到另一个对象(相同类型)时,我从 ToString() 方法中取回了 Type()。

System.Collections.Specialized.NameValueCollection variables = new System.Collections.Specialized.NameValueCollection(Request.QueryString);
if (!string.IsNullOrEmpty(variables["sid"]))
    variables.Remove("sid");
Response.Write(variables.ToString());

Is there a tidier way to output it rather than looking and building the string manually?

是否有一种更整洁的方式来输出它而不是手动查找和构建字符串?

采纳答案by Igal Tabachnik

You can also use Reflector to extract the HttpValueCollectionclass into your own, and use it then.

您也可以使用 Reflector 将HttpValueCollection类提取到您自己的类中,然后使用它。

回答by Johannes H?drich

Why do you want to copy the QueryString collection into a new NameValueCollection?

为什么要将 QueryString 集合复制到新的 NameValueCollection 中?

    if (!string.IsNullOrEmpty(Request.QueryString["sid"]))
        Request.QueryString.Remove("sid");

Yes indeed, i am wrong, it is read only. So the essence is to use the Remove Method on your NameValuecollection:

是的,我错了,它是只读的。所以本质是在你的 NameValuecollection 上使用 Remove 方法:

System.Collections.Specialized.NameValueCollection variables = new System.Collections.Specialized.NameValueCollection(Request.QueryString);
if (!string.IsNullOrEmpty(variables["sid"]))
    variables.Remove("sid");

回答by James Curran

Request.QueryString actually return a HttpValueCollection object (which unfortuately, is internal to System.Web so you can't you it).

Request.QueryString 实际上返回一个 HttpValueCollection 对象(不幸的是,它是 System.Web 的内部对象,因此您不能使用它)。

Nevertheless, HttpValueCollection is derived from NameValueCollection, and it's Remove() method remains intact, so you should be able to call Request.QueryString.Remove("sid");

尽管如此,HttpValueCollection 是从 NameValueCollection 派生的,它的 Remove() 方法保持不变,所以你应该能够调用 Request.QueryString.Remove("sid");

回答by Brian Schmitt

Because it is actually a special NVC that is of type HTTPValueCollection. So when you call .ToString on it, it knows how to format it correctly.

因为它实际上是一个 HTTPValueCollection 类型的特殊 NVC。所以当你调用 .ToString 时,它知道如何正确格式化它。

回答by Jimmy

If you don't absolutely need a NameValueCollection, A dictionary offers a lot of the same semantics:

如果你不是绝对需要 NameValueCollection,字典提供了很多相同的语义:

var variables = Request.QueryString.OfType<DictionaryEntry>()
    .Where(entry => entry.Key != "sid")
    .ToDictionary(entry => entry.Key, entry => entry.Value);

回答by Michele Bersini

HttpValueCollection is internal, but you can use "var" to declare it without extract it with reflector.

HttpValueCollection 是内部的,但您可以使用“var”来声明它,而无需使用反射器提取它。

var query = HttpUtility.ParseQueryString(Request.Url.Query);
query["Lang"] = myLanguage; // Add or replace param
string myNewUrl = Request.Url.AbsolutePath + "?" + query;