UrlEncoding

Namespace: SnowBank.Text · class

Helper for encoding/decoding URIs

Remarks

This method is intended to be used when System.Web.HttpUtility.dll is not available

Methods

Decode

static string Decode(string value, Encoding encoding = null)

Decodes a text string encoded as a URL (%XX)

  • value — String containing encoded text
  • encoding — Encoding used (defaults to UTF-8 if null)

Returns: Decoded string

static string Decode(string value, int offset, int count, Encoding encoding = null)

Decodes a section of a text string encoded as a URL (%XX)

  • value — String containing a URI or any other text encoded as a URL
  • offset — Offset from the start of the string
  • count — Number of characters to decode
  • encoding — Encoding used (defaults to UTF-8 if null)

Returns: Decoded section of the string

EncodeData

static string EncodeData(string value, Encoding encoding = null)

Encodes a value that will be used as a value in a QueryString

  • value — Value to encode correctly (' ' => '+')
  • encoding — Optional encoding (UTF-8 by default)

Returns: Text that can be used as a value in a QueryString

EncodeData("foo bar/baz") => "foo+bar%2fbaz"

EncodeDataObject

static string EncodeDataObject(object value, Encoding encoding = null)

EncodePath

static string EncodePath(string value)

Encodes a value that will be used as a segment of a URI path

  • value — Value to encode correctly (' ' => '%20')

Returns: Text that can be embedded in a URI path

EncodePath("foo bar/baz") => "foo%20bar%2fbaz"

EncodePathObject

static string EncodePathObject(object value)

EncodeUri

static string EncodeUri(string value)

Properly encodes a URI that may be malformed

  • value — Uri to encode correctly

Returns: Correctly-encoded Uri

Does not touch the query string if there is one!

EncodeUri("http://server/path to the/file.ext?blah=xxxx") => "http://server/path%20to%20the/file.ext?blah=xxx"

ParseQueryString

static NameValueCollection ParseQueryString(string qs, Encoding encoding = null)

Parses a QueryString, and returns the list of parameters found

  • qs — QueryString to parse (in the form 'name1=value1&name2=value2&...')
  • encoding — Encoding used (defaults to UTF-8 if null)

Returns: NameValueCollection containing the parameters of the querystring

"foo&..." will contain null, "foo=&..." will contain String.Empty