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

NameValueCollection.ToUrlString

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

Source

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()

Author: Joel Mueller

Submitted on: 29 okt 2010

Language: F#

Type: System.Collections.Specialized.NameValueCollection

Views: 4731