About this article

Written by:
Posted:
14/01/2012 13:01:30

About the author

Imar Spaanjaars is the owner of De Vier Koeden, a company specializing in consultancy and development on the Microsoft .NET platform and DynamicWeb.

Interested in custom development or consultancy on DynamicWeb or .NET? Then contact De Vier Koeden through the Contact page.

If you want to find out more about Imar Spaanjaars, check out his About page on this web site or check out his personal website.

Custom Modules - Part 10 - Implementing URL providers

In earlier versions of Dynamicweb you custom modules could benefit from SEO friendly URLs using the "Convert Module URLs" option in the Customized URLs Configuration Panel. While this worked well for quick and dirty scenarios, you had no control over the way the URL was built. In addition, the URL used funky characters such as [ and ] to separate query string values. And finally, the URL still contained the ID and other data your module needed in order to do its work.

With the introduction of the URL Providers, these disadvantages have now all been solved.

Implementing a URL Provider

A URL Provider in Dynamicweb takes care of converting URLs from and to a search engine (and user) friendly format. For example, a public URL such as title-of-news-article.aspx can internally be rewritten as &ArticleId=123 so your custom news module understands it should render the article with an ID of 123. To implement your own provider for your custom modules (or for existing Dynamicweb content), follow these steps:

  1. Create a class that inherits UrlProvider.
  2. Override the method GetMappings() and return an instance of List<Mapping>. A Mapping instance stores a few important properties of your URL:
    1. The query string name (such as ArticleId)
    2. The query string value (such as 123)
    3. The friendly URL you want to associate with this Query String value (such as title-of-news-article)
  3. Decorate the UrlProvider with a few attributes to tell Dynamicweb more about the actual provider.
  4. Register the URL Provider in the Management Center.

I'll show you how to implement a URL Provider for the Custom News module built throughout this article series on Custom Modules in the following section.

  1. In your Custom Modules project, create a new folder called Providers. This name is not required, but it helps to keep your project organized.
  2. Add a new class file to this folder and call it ArticlesUrlProvider.
  3. Let the class inherit Dynamicweb’s UrlProvider
  4. Override the GetMappings method. Inside this method, get a list of all available news articles and then add their data to a List<Mapping> instance, providing ArticleId as the query string parameter, the id of the article as the value, and a cleaned up version of the URL as the name.
  5. You should end up with code like this:
using System;
using System.Collections.Generic;
using System.Linq;
using Dynamicweb;
using Dynamicweb.Extensibility;
using Dynamicweb.Frontend.UrlProviders;
using Dynamicweb.Samples.Web.Model;
namespace Dynamicweb.Samples.Web.DvkArticles
{
  [AddInName("My Custom News Articles"),
  AddInDescription("Generates urls for articles in the format /article-title.aspx."),
  AddInActive(true), AddInGroup("CustomArticles")]
  public class ArticlesUrlProvider : UrlProvider
  {
    /// <summary>
    /// Gets the mappings for the list of articles in the system.
    /// </summary>
    public override List<Mapping> GetMappings()
    {
      if (!Base.IsModuleInstalled("CustomNews"))
      {
        // No point in getting URLs when the module is not installed.
        return null;
      }
      List<Mapping> list = new List<Mapping>();
      using (CustomModulesSamplesEntities db = new CustomModulesSamplesEntities())
      {
        foreach (var article in db.Articles.Where(c => (c.PublicationDateFrom <= DateTime.Now 
             && c.PublicationDateTo >= DateTime.Now && !c.Deleted)))
        {
          string articleTitle = article.Title.Trim().Replace(" ", "-").ToLower();
          list.Add(new Mapping("ArticleId", article.Id.ToString(), articleTitle));
        }
      }
      return list;
    }
  }
}
  1. Compile your solution and resolve any compilation errors you may get.
  2. Log in to your Dynamicweb site and open the Management Center. Click Customized URLs and configure the screen as follows:


  3. Notice how the name and description are retrieved from the AddInName and AddInDescription attributes that have been added to the UrlProvider class.

  1. Browse to the frontend of your web site and open the page with your custom article module on it. Notice how the links to the details page of the articles are now SEO friendly and include the article's title, rather than URLs containing something like &ArticleId=123 that was used previously.

 

By clicking 'Accept All' you consent that we may collect information about you for various purposes, including: Statistics and Marketing