EPiWiki.se  - EPiServer notes shared with others
 

Snipplet to create a minimal Friendly URL handler

[Edit]
The friendly URL handling in EPiServer CMS 5 is pretty hard to understand so I wrote a sample code how to implement a minimal friendly URL handler to add “/Mattias“ at start of every request.
Eg.
http://localhost/en/Events/
is rewritten to
http://localhost/Mattias/en/Events/
can be handy if the customer really needs to have the geographical location at the start of the URLs path (or if you want to pimp your site with the original start path “/Mattias” of cause).

Global.asax.cs


You have to attach to the event handler for friendly URLs this is done in Global.asax.cs.

protected void Application_Start(Object sender, EventArgs e)
{
    XFormControl.ControlSetup += new EventHandler(XForm_ControlSetup);

    EPiServer.Web.UrlRewriteModuleBase.HttpRewriteInit +=
         new EventHandler<EPiServer.Web.UrlRewriteEventArgs>(
             MyFriendlyURL.UrlRewriteModuleBase_HttpRewriteInit);

    EPiServer.Web.HtmlRewriteToExternal.HtmlRewriteInit +=
        new EventHandler<EPiServer.Web.HtmlRewriteEventArgs>(
            MyFriendlyURL.HtmlRewriteToExternal_HtmlRewriteInit);
}

MyFriendlyUrl.cs


The main URL rewriter code.

using System;
using System.Text.RegularExpressions;
using EPiServer.Web;

namespace EPiServer
{
    public class MyFriendlyUrl
    {
        /// <summary>
        /// Handles the HttpRewriteInit event to initialize the
        /// UrlRewriteModuleBase control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EPiServer.Web.UrlRewriteEventArgs"/>
        /// instance containing the event data.</param>
        public static void UrlRewriteModuleBase_HttpRewriteInit(
            object sender,
            UrlRewriteEventArgs e)
        {
            UrlRewriteModuleBase module = sender as UrlRewriteModuleBase;
            if (module != null)
            {
                module.HttpRewritingToInternal +=
                    new EventHandler<UrlRewriteEventArgs>(
                        HttpRewritingToInternal);
            }
        }

        /// <summary>
        /// Handles the HtmlRewriteInit event of initializate the
        /// HtmlRewriteToExternal control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EPiServer.Web.HtmlRewriteEventArgs"/>
        /// instance containing the event data.</param>
        public static void HtmlRewriteToExternal_HtmlRewriteInit(
            object sender,
            HtmlRewriteEventArgs e)
        {
            MyUrlRewriter rewriter = new MyUrlRewriter();
            e.RewritePipe.HtmlRewriteUrl +=
                new EventHandler<HtmlRewriteEventArgs>(
                    rewriter.RewritePipe_HtmlRewriteUrl);
        }

        /// <summary>
        /// A handler to rewrite external URLS (friendly) the EPiServers
        /// native urls (with query srings).
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EPiServer.Web.UrlRewriteEventArgs"/>
        /// instance containing the event data.</param>
        public static void HttpRewritingToInternal(
            object sender,
            UrlRewriteEventArgs e)
        {
            UrlRewriteModule module = sender as UrlRewriteModule;

            if (module == null || !UrlRewriteProvider.IsFurlEnabled)
            {
                // Break if the sender isn't a rewiter
                // or when friendly URLs is not turned on
                return;
            }

            UrlBuilder ub = new UrlBuilder(e.Url);
            // Remove /Mattias from the request
            e.UrlContext.InternalUrl.Path =
                Regex.Replace(ub.Path, "^/Mattias", "", RegexOptions.IgnoreCase);
            e.UrlContext.InternalUrl.QueryCollection = ub.QueryCollection;
            // Mark it modified, but not cancel the event
            e.IsModified = true;
        }
    }

    public class MyUrlRewriter
    {
        /// <summary>
        /// Handler to rewrite native Urls with query parameters to
        /// friendly urls
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EPiServer.Web.HtmlRewriteEventArgs"/>
        /// instance containing the event data.</param>
        public void RewritePipe_HtmlRewriteUrl(
            object sender,
            HtmlRewriteEventArgs e)
        {
            try
            {
                //Add "/Mattias" to the outgoing data.
                e.ValueBuilder.Insert(0, "/Mattias");
            }
            catch
            {
                // You should never catch all exception, but this is an example.
                // Important is that never throw an exception in the
                // friendly URL handling.
            }
        }
    }
}
Version author:
Mattias Lövström

EPiServer version

'EPiServer CMS 5'