Make The HttpValueCollection Class Public And Move It To System.DLL

I find the System.Web.HttpValueCollection class very useful in a wide number of situations that involve composing HTTP requests or any other need to represent name/value collection as a string (in an XML attribute, for example).

As of now (.NET Framework 3.5 SP1 Beta), the only way to create an instance of the System.Web.HttpValueCollection class is using the System.Web.HttpUtility.ParseQueryString method.

I’d like to see it public and available in a more generic assembly like System.DLL to be available on every type of .NET application (Windows Client applications, Windows Service applications, Silverlight applications, Windows Mobile applications, etc.).

If you agree with me, vote on my suggestion on Microsoft Connect.

7 Responses to Make The HttpValueCollection Class Public And Move It To System.DLL

  • PeterRitchie says:

    What sort of things do you want to do with HttpValueCollection? It’s essentially just a NameValueCollection–or is there something specific in the HttpValueCollection interface that you’re looking to get access to?

    Is it more a problem with referencing the System.Web assembly?

    It’s not really a solution; but you can always access an internal class via reflection:
    Type t = Type.GetType(“System.Web.HttpValueCollection, System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”);
    ConstructorInfo constructorInfo = t.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[]{typeof(String), typeof(Boolean), typeof(Boolean), typeof(Encoding)}, null);
    NameValueCollection o = constructorInfo.Invoke(new object[] { “a=1&b=2”, false, true, Encoding.UTF8}) as NameValueCollection;

    …which could be in a wrapper class like HttpValueCollectionWrapper…

  • paulo says:

    Just look into it with Reflector and you’ll see.

    I want to be able to build a new read-only instance.

    I want to be able to build a new empty instance without having to parse an empty string.

    I want to be able to parse a string using a specified encoding.

    I want to convert the collection to a string choosing to URL-encode or not.

    I want to convert the collection to a string specifying the keys to exclude.

    The HttpUtility.ParseQueryString method was introduced in version 2.0 but the HttpValueCollection class was in the framework since version 1.1 (and probably 1.0). If it was found useful to parse a query string form outside of the System.Web assembly, why not expose the full set of features.

    I’ve implemented classes like this several projects whenever I need to specify a list or a name/value collection as a string. Why would I invent another encoding or this if one has already been invented?

    With the CLR in SQL Server I can pass a list or dictionary to a stored procedure and have it expanded to a table inside the database.

    You need the ReflectionPermition to get non public constructors. You also need System.Web.dll and you don’t have it in the upcoming client profile or the compact framework, for example.

  • hbz says:

    “I want to convert the collection to a string specifying the keys to exclude.”

    Agreed. This is especially useful with URL rewriting when you often need to manipulate the querystring.


  • Alan Singfield says:

    To use the very useful .ToString() method of the HttpValueCollection, do this:

    NameValueCollection nvc = HttpUtility.ParseQueryString(“”);

    nvc[“a”] = “123”;
    nvc[“b”] = “456”;


    // Returns a=123&b=456

  • paulo says:

    You’re right, Alan. But I fail to see your point.

  • Tim Howland says:

    Paulo, I’m very grateful for Alan’s post, showing me that I can do without HttpValueCollection, as NameValueCollection provides all I need.

  • paulo says:

    What post, Tim?