Wildcards and Data Driven URLs

Wildcard items in Sitecore are a convenient way to handle dynamic URLs. They let you pass data through the URL instead of relying on query string values that are appended to the URL. Then it is up to you to figure out where that data is in the URL, which is nothing difficult, but if you have a lot of wildcards in your site and a lot of controls that depend on them, this can become a management headache.

Before I get into the Wildcard Module, I want to explain the problem the module is designed to address. As an example, I will use a site that displays a list of blogs. The blogs are managed by an external system, but I want to integrate that external data with my Sitecore site.

The list of blogs should appear at http://localhost/blogs.aspx. I have a control on this item that reads the external data and generates links for each blog. When a visitor clicks the blog link, a Sitecore page displays details about that specific blog.

The external system provides an API that exposes all of this information. In order to retrieve details about a specific blog, the API requires I identify the group name and the blog name. This means I have 2 parameters I need to pass. For SEO reasons, I want the links to look like the following:

http://localhost/blogs/travel/euroblog.aspx

http://localhost/blogs/nightlife/nycitylife.aspx

The Wildcard Module is designed to address 2 challenges presented by this example:

How to generate the URLs that include all of the required parameters.

How to read the parameters from the generated URLs.

The Wildcard Module allows you to define routes to wildcard items. A route is a set of rules that explain how to handle variables (wildcard items) that appear in an item's path, and which items the rules apply to.

In this example, the rules are:

Replace the first wildcard with the blog's group name

Replace the second wildcard with the blog's name

The Sitecore item that the route applies to is located at /sitecore/content/home/Wildcard Module Examples/Blogs/*/*.

The following screenshot shows what a route looks like in the Content Editor.

Once this route is defined, I can use it to generate the dynamic URLs and to read the parameters from the URL. The following code shows how external data can be used to generate the dynamic URLs:

Are there any extra steps to get wildcards working for cultures? For example I set up a route and it works fine with base English(en) but when I switch the culture to US English(en-US) the tokens are not being found.

After pounding away I found the answer to my question above regarding cultures. The tokens I created were not being recognized when the Url had an uppercase culture for example en-US. When I set the Url ToLower() the tokens are recognized. Old code: var myToken = GetTokenValue(Sitecore.Context.Item, Sitecore.Context.Site, token, url) ?? ""; New code: var myToken = GetTokenValue(Sitecore.Context.Item, Sitecore.Context.Site, token, url.ToLower()) ?? ""; Thanks Adam!