NameValueCollection.ToUrlString

namespace Foo

open System
open System.Collections.Specialized
open System.Web

[<AutoOpen>]
module FsExtensions =

    type NameValueCollection with
        // Converts the collection to a URL-formatted query string (the part after the question mark).
        member this.ToUrlString() =
            let encode = HttpUtility.UrlEncode
            // a key can have multiple values, so flatten this out, repeating the key if necessary
            let getValues (key:string) =
                let kEnc = (encode key) + "="
                this.GetValues(key)
                |> Seq.map (fun v -> kEnc + (encode v))

            let pairs = this.AllKeys |> Seq.collect getValues
            
            String.Join("&", pairs)
Example:
let url = Request.Form.ToUrlString()

Description

Converts a NameValueCollection (such as Request.Form) into a url-formatted string of name/value pairs.

Details

Double click on the code to select all.

 

;