Yesterday I read a blog post by Lars Timenes, describing how one
could easily generate a
bit more friendly URL Segments in EPiServer. (Just an
observation, but the EPiServer site hosting the blog post, doesn't
seem to use his code.) His example uses the CMS 5 style of hooking
into events, so I thought I'd give an example of how to do it in
CMS 6 and the new Initialization engine. The back story here, is
that sites like Google, sees all lower case URLs, and mixed case
URLs as different URLs. Although this can be fixed by adding a <link
rel="canonical" /> tag to your markup, I like to have more
than one solution to things.
In order to get this to work, you'll need to add references to
the EPiServer.Framework.dll and the
System.ComponentModel.Composition.dll, both installed with CMS 6
and located in the "C:\Program Files
(x86)\EPiServer\Framework\6.0.x.x\bin" directory.
Oh, and as a side note, Yes, I do write all those tedious
documentation comments in my production code. It's how I do things.
Sorry for being a goody two-shoes.
Edit: After reading Magnus's comment, I added the simple
code to detach the event.
Edit: Karl, case doesn't matter to the server, but
search engines may or may not (it's entirely up to them, not me),
index URLs with different casing as different pages, severly
affecting the PageRank of the URL in question. That's why I want my
URLs to display consistently, and if I always render them in
lowercase, I doubt someone will try to link to my site using a
mixed-case URL of their own device.
using System;
using EPiServer;
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using EPiServer.Web;
namespace Development {
/// <summary>
/// An <see cref="EPiServer.Framework.IInitializableModule" /> enabling hooking into EPiServer events.
/// </summary>
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class ModuleInitializer : IInitializableModule {
#region IInitializableModule Members
/// <summary>
/// <para>Initializes the Module and attaches custom eventhandlers.</para>
/// <para>Attaches a <see cref="System.EventHandler" /> for the EPiServer.Web.UrlSegment.
/// CreatedUrlSegment event.</para>
/// </summary>
/// <param name="context">The current
/// <see cref="EPiServer.Framework.Initialization.InitializationEngine"/></param>
public void Initialize(InitializationEngine context) {
UrlSegment.CreatedUrlSegment +=
new EventHandler<UrlSegmentEventArgs>(UrlSegment_CreatedUrlSegment);
}
/// <summary>
/// Makes sure that all URL Segments are always lowercase, for increased SEO performance.
/// </summary>
void UrlSegment_CreatedUrlSegment(object sender, UrlSegmentEventArgs e) {
e.PageData.URLSegment = e.PageData.URLSegment.ToLowerInvariant();
}
/// <summary>
/// Perform pre-loading tasks.
/// </summary>
public void Preload(string[] parameters) {
}
/// <summary>
/// <para>Uninitializes the Module.</para>
/// <para>Detaches the <see cref="System.EventHandler" /> for the EPiServer.Web.UrlSegment.
/// CreatedUrlSegment event added during Initialization.</para>
/// </summary>
/// <param name="context">The current
/// <see cref="EPiServer.Framework.Initialization.InitializationEngine"/></param>
public void Uninitialize(InitializationEngine context) {
UrlSegment.CreatedUrlSegment -=
new EventHandler<UrlSegmentEventArgs>(UrlSegment_CreatedUrlSegment);
}
#endregion
}
}