// Copyright (c) Service Stack LLC. All Rights Reserved. // License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt using System.Collections.Generic; namespace ServiceStack { public static class NameValueCollectionExtensions { #if !SILVERLIGHT && !MONOTOUCH && !XBOX public static Dictionary ToDictionary(this System.Collections.Specialized.NameValueCollection nameValues) { if (nameValues == null) return new Dictionary(); var map = new Dictionary(); foreach (var key in nameValues.AllKeys) { if (key == null) { //occurs when no value is specified, e.g. 'path/to/page?debug' //throw new ArgumentNullException("key", "nameValues: " + nameValues); continue; } var values = nameValues.GetValues(key); if (values != null && values.Length > 0) { map[key] = values[0]; } } return map; } public static System.Collections.Specialized.NameValueCollection ToNameValueCollection(this Dictionary map) { if (map == null) return new System.Collections.Specialized.NameValueCollection(); var nameValues = new System.Collections.Specialized.NameValueCollection(); foreach (var item in map) { nameValues.Add(item.Key, item.Value); } return nameValues; } #endif } }