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:
- Create a class that inherits UrlProvider.
- Override the method GetMappings() and return an instance of List<Mapping>. A Mapping instance stores a few important properties of your URL:
- The query string name (such as ArticleId)
- The query string value (such as 123)
- The friendly URL you want to associate with this Query String value (such as title-of-news-article)
- Decorate the UrlProvider with a few attributes to tell Dynamicweb more about the actual provider.
- 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.
- In your Custom Modules project, create a new folder called Providers. This name is not required, but it helps to keep your project organized.
- Add a new class file to this folder and call it ArticlesUrlProvider.
- Let the class inherit Dynamicweb’s UrlProvider
- 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.
- 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; } } }
- Compile your solution and resolve any compilation errors you may get.
- Log in to your Dynamicweb site and open the Management Center. Click Customized URLs and configure the screen as follows:
Notice how the name and description are retrieved from the AddInName and AddInDescription attributes that have been added to the UrlProvider class.
- 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.