How does Orchard make "pretty urls"?

Topics: General
Oct 20, 2014 at 1:24 AM
Edited Oct 20, 2014 at 1:52 AM
How does Orchard do this? For example, if the title is "Nice Looking Slug" then it becomes:

www.mysite.com/nice-looking-slug

Can you point me to the source code, or show me how Orchard does it?
Oct 20, 2014 at 5:17 PM
Edited Oct 20, 2014 at 5:18 PM
Hi!

Orchard takes advantage of the normal asp.net MVC Routing. Take a look at Orchard.AutoRoute and Orchard.Alias.

It get's all defined routes from the database and adds them to the RouteCollection.

Orchard.Alias/Routes.cs
public void GetRoutes(ICollection<RouteDescriptor> routes) {
    foreach (RouteDescriptor routeDescriptor in GetRoutes()) {
        routes.Add(routeDescriptor);
    }
}

public IEnumerable<RouteDescriptor> GetRoutes() {
    var distinctAreaNames = _blueprint.Controllers
        .Select(controllerBlueprint => controllerBlueprint.AreaName)
        .Distinct();

    return distinctAreaNames.Select(areaName =>
        new RouteDescriptor {
            Priority = 80,
            Route = new AliasRoute(_aliasHolder, areaName, new MvcRouteHandler())
        }).ToList();
}
Oct 20, 2014 at 5:20 PM
It is called 'Slugify'. Basically you just take the string, lowercase all letters and replace the spaces with something else like a '-'.

In pseudo code it would look something like this:
title 
    .ToLowerCase()
    .ReplaceAll(" ", "-")
Pretty simple. Then just store the slug with your item, and write a method that retrieves the item that is stored with that particular slug.