Manipulating Query String in CSharp Blog post

If you're an ASP.NET developer then sooner or later you will come across an occasion when you want to manipulate or parse values from the current request's querystring (the querystring being the name/value pairs you often see appended to URLs).

The good news is that ASP.NET pages makes available the HttpRequest.QueryString property. The bad news is that this returns a read-only NameValueCollection that cannot be easily manipulated or even iterated over (you can't, for instance, use the foreach construct with a NameValueCollection). This means that parsing values in the querystring becomes a real chore.

To this end I developed a very small C# library (that can be found on GitHub) that enables developers to easily manipulate the querystring collection. It has useful methods to add, remove, replace, count and parse values within a querystring. Instead of outlining all the methods in detail I will show a small code excerpt below that uses the library to perform a few common tasks. If you're like me, you'll find this way is the best way to grasp how it works.


3 Comments


James Jackson-South Avatar

I like this, especially that it takes a delegate function.


I wrote something kinda similar for ImageProcessor.Web to parse the various values that are available as parameters. I added a lot of TypeConverters to allow me to convert a whole bunch of extra types outwith the normal IConvertable range. I wonder whether there is anything I can borrow from your code.


ImageProcessor.Web/Helpers/QuerystringParser/QueryParamParser.cs


Dan Diplo Avatar

Thanks, James. I'd be honoured if you found anything in the code of use, though I'm not at your level when it comes to coding. But it's cool to see how you do stuff - hopefully we can both learn :)


James Jackson-South Avatar

Ha I'm sure I can learn plenty from you. I'm all smoke and mirrors :)

Just fill in the form and click Submit. But note all comments are moderated, so spare the viagra spam!

Tip: You can use Markdown syntax within comments.