Paulo Morgado

.NET Development & Architecture

This Blog

Syndication

Search

Sponsored By

Tags

News

Unit Test Today! Get Typemock Isolator!

Books

 

Events

Visitors

Visitor Locations

Community

Email Notifications

Archives

Profile

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.

Published Tue, Jul 15 2008 1:22 by Paulo Morgado

Comments

# re: Make The HttpValueCollection Class Public And Move It To System.DLL@ Tuesday, July 15, 2008 9:06 AM

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=2.0.0.0, 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...

PeterRitchie

# re: Make The HttpValueCollection Class Public And Move It To System.DLL@ Tuesday, July 15, 2008 3:06 PM

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.

Paulo Morgado

# asp.net: Modify Request.QueryString « Stephen Sulzberger’s Blog@ Friday, October 03, 2008 12:42 AM

Pingback from  asp.net: Modify Request.QueryString « Stephen Sulzberger’s Blog

asp.net: Modify Request.QueryString « Stephen Sulzberger’s Blog

# Agreed@ Sunday, April 19, 2009 2:34 PM

"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.

hbz

hbz

# re: Make The HttpValueCollection Class Public And Move It To System.DLL@ Friday, November 20, 2009 3:27 AM

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

NameValueCollection nvc = HttpUtility.ParseQueryString("");

nvc["a"] = "123";

nvc["b"] = "456";

Debug.WriteLine(nvc.ToString());

// Returns     a=123&b=456

Alan Singfield

# re: Make The HttpValueCollection Class Public And Move It To System.DLL@ Saturday, November 21, 2009 11:41 AM

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

Paulo Morgado

Leave a Comment

(required) 
(required) 
(optional)
(required)