ExtensionMethod.NET Home of 841 C#, Visual Basic, F# and Javascript extension methods


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


namespace Foo

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

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) + "="
    |> Seq.map (fun v -> kEnc + (encode v))

let pairs = this.AllKeys |> Seq.collect getValues

String.Join("&", pairs)


let url = Request.Form.ToUrlString()

Author: Joel Mueller

Submitted on: 29 okt 2010

Language: F#

Type: System.Collections.Specialized.NameValueCollection

Views: 4731