The WebAddress Object
In my last weblog entry, I mentioned that the WebContext object allowed code to get interesting objects directly by name, rather than having to “pick out” an object from a collection by knowing its key. For example, to discover the Owner of the current website, the code can get it directly from WebContext:
Owner owner = WebContext.Current.Owner;
If I had not used a custom WebContext object, and instead used the intrinsic HttpContext object, then to discover the Owner of the current website, the code would have to “pick out” an item from the HttpContext Items property by using a key value:
Owner owner = (Owner)HttpContext.Current.Items["Owner"];
The simplicity afforded by the WebContext object prompted me to do the same thing for the address of a webpage. I created a custom WebAddress object that allowed code to get interesting objects directly by name, rather than having to figure them out by messing with details from the HttpRequest.Current object. First, I decided upon a convention for the addresses that a Charlie website would use:
www.example.com/culture/path/document/parameter?query=string
A concrete example might look as follows:
www.example.com/fr-ca/personal/photos/wedding
If a request for the above address were received, Charlie’s WebAddress object will make the separate elements of the address immediately available. For example:
WebAddress webAddress = WebRequest.Current.WebAddress; String culture = webAddress.Culture; // "/fr-ca" String path = webAddress.Path; // "/personal" String document = webAddress.Document; // "/photos" String parameter = webAddress.Parameter; // "/wedding"
In addition, the WebAddress object will provide combined values. For example:
WebAddress webAddress = WebRequest.Current.WebAddress; String part1 = webAddress.PathAndDocument; // "/personal/photos" String part2 = webAddress.DocumentAndParameter; // "/photos/wedding"
This is very simple string manipulation, so why bother? The benefit comes when the code needs to determine a related web address. For example, the above example shows the Canadian French version of the wedding photos page. Say that the page provides a set of flag links that take the visitor to the same page, but in another language. Simple:
String currentAddressWithoutCulture = WebRequest.Current.WebAddress.PathAndDocumentAndParameter; BritishLink.NavigateUrl = "/en-gb" + currentAddressWithoutCulture; AmericanLink.NavigateUrl = "/en-us" + currentAddressWithoutCulture; FrenchLink.NavigateUrl = "/fr-fr" + currentAddressWithoutCulture;
The WebAddress is simply another handy object that has been added to Charlie’s Web System.
You may be wondering just how realistic is the web address I have been using. After all, it has no .aspx extension anywhere. Even more unusual, the document is stuck in the middle of the address, and not at its end. I’ll come back to these aspects of the address when I discuss friendly URLs.
by Alister Jones | Next up: The WebContext Builder →
----
Post a Comment