Best practices to get a content item from it's route

Topics: Writing modules
Jun 26, 2013 at 3:31 PM
Edited Jun 26, 2013 at 3:35 PM
I have created a controller and Item action for my own Course content type which overwrites the functionality of the default ItemController Display action. Because I want a bit more control over the displaying of a Course.

The only difference between my Item action and the Display action is that the Display action receives the ID for the content item as a parameter to view and my Item action gets the URL path as a parameter.

I do not know how the ItemController gets the ID for the content item from the URL path - something happens before the Display action is hit but I do not know what - maybe someone can explain this?

So as I just have the URL path, I do this to return the ContentItem from the path
    return _contentManager
        .Query()
        .ForType("Course")
        .ForVersion(VersionOptions.Published)
        .Where<AutoroutePartRecord>(a => a.DisplayAlias != null && a.DisplayAlias == autoRouteDisplayAlias)
        .List()
        .FirstOrDefault();
This query does return me a contentItem, but will it always return the most recent version of the correct one?

I ask because I have recently been looking into the AliasPart and I am confused as to how the AliasPart and the AutoroutePart differ when getting a contentitem from it's route - can anyone help?
Developer
Jun 26, 2013 at 4:23 PM
Jun 26, 2013 at 5:07 PM
Nice blog post, but the case I am looking at is where I have the "/about-us" URL path and I want the ID of the content item. I do not have the ID in the request.

So I need to plug into AliasRoute within my controller action, but I don't know how to do this - any ideas?
Developer
Jun 26, 2013 at 5:26 PM
Look at AliasRoute to see how it's mapping an alias to an ID.