Urls

It is very common to dynamically build or modify urls or their query strings. Sometimes it is also necessary to parse an url to extract information from it. OpenWaves.Url class and its buddies can provide the necessary API to achieve both.

Parsing urls

To parse a url string use the following code.
using OpenWaves;

...

var url = Url.Parse("http://host.com/firstPathSegment/secondPathSegment?Param1=Value1&Param2=Value2#fragmentString");

//
// Now we can access all of the url properties
//

var firstPathSegment = url.Path.Segments[0];
var secondPathSegment = url.Path.Segments[1];
var fragment = url.Fragment;
var param1 = url.Query["Param1"];
var param2 = url.Query["Param2"];

//
// Above properties are common for both Absolute and Relative urls. 
// If we want to access schema, host, or port properties we need to make sure the url is absolute
//

if (url is AbsoluteUrl)
{   
    var absoluteUrl = (AbsoluteUrl)url;
    var schema = absoluteUrl.Schema;
    var host = absoluteUrl.Host;
    var port = absoluteUrl.Port;
}

//
// If we want to explicitly parse AbsoluteUrl or RelativeUrl we can use the syntax below
//

var absoluteUrl = Url.ParseAbsolute("...");
var relativeUrl = Url.ParseRelative("...");

//
// All of the Parse methods above have a version that does not throw exceptions on invalid urls
//

var success = Url.TryParse("...", out url);
var success = Url.TryParseAbsolute("...", out absoluteUrl);
var success = Url.TryParseRelative("...", out relativeUrl);

Building urls

All url classes are immutable - that is cannot be used to modify urls. To modify the url one needs to obtain an instance of UrlBuilder class. The snippets below illustrates how to use the builder class.

using OpenWaves;

...

var builder = url.GetBuilder();
builder.Path = UrlPath.Parse("foo/bar/foobar");
builder.Query["Param1"] = value1; // value can be an object, UrlBuilder will use Url.Encode(value1.ToString())
builder.Fragment = "Chapter1";

//
// For absolute urls
//
var builder = absoluteUrl.GetBuilder();
builder.Host = "www.google.com";
builder.Port = 81;

//
// To get modified url instance or just a string representation of the url 
//

var url = builder.ToUrl();var urlString = builder.ToString();

To make it easier to work with UrlBuilder class a set of extension methods has been created that expose a fluent interface to the builders.

using OpenWaves;

...

var newUrl = url.GetBuilder()
    .SetPath(UrlPath.Parse("foo/bar/foobar"))
    .AddParameter("Param1", value1)
    .SetFragment("Chapter1")
    .ToUrl();

Relative url resolution

var baseUrl = Url.ParseAbsolute("http://foo.com/demos/pacman");

var relativeUrl = Url.Parse("styles/master.css");
var rootedUrl = Url.Parse("/games/pacman"");
var absoluteUrl = Url.Parse("http://bar.com/movies");

relativeUrl.ResolveAgainst(baseUrl); // returns "http://foo.com/demos/styles/master.css"
rootedUrl.ResolveAgainst(baseUrl); // returns "http://foo.com/games/pacman"
absoluteUrl.ResolveAgainst(baseUrl); // returns "http://bar.com/movies"

Last edited Jul 13, 2011 at 6:53 AM by mgrzyb, version 7

Comments

No comments yet.