Try our conversational search powered by Generative AI!

routing - removing a segment from the url


Hi Guys

i've been struggling to get some custom routing working.

Basically i need to remove various elements from the url for more friendly urls...

so for example instead of /markets/insurance/products/motor/... and /markets/insurance/products/home/.. etc i just have /insurance/motor/..  /insurance/home/.. etc

there are a few threads about routing but none of them provide enough for me to get a solution working. I've already had a good look at these

I'd be really grateful if someone could provide some pointers or code examples

many thanks










Jul 15, 2013 19:57

No response yet, so i'll add some details... I've tried a few things, i think i'm close, but so far no cigar! Has anyone done this already? think i'm just missing something obvious!

i saw this...

"During outgoing URL generation are the events CreatingVirtualPath and CreatedVirtualPath raised where event handlers can modify the URLs generated

An alternative could be to hook up to event ContentRoute.CreatedVirtualPath and modify the generated Url.

This would probably be a good approach if you have an easy recogizable pattern of the url you want to modify."

so i created an IInitializableModule - added an eventhandler ContentRoute.CreatedVirtualPath += CreatedVirtualPath;

void CreatedVirtualPath(object sender, UrlBuilderEventArgs e)
          e.UrlBuilder.Path = e.UrlBuilder.Path.Replace("markets/","");

that certainly changed the urls - but just left me with 404 errors

also tried a segment, do i need to implement GetVirtualPathSegment? i couldnt work out how i could reformat the url in there

in global.asax.cs

protected override void RegisterRoutes(RouteCollection routes)
    var segment = new MyCustomSegment("customnode");
    var routingParameters = new MapContentRouteParameters()
        SegmentMappings = new Dictionary<string, ISegment>()
    routingParameters.SegmentMappings.Add("customnode", segment);

    name: "customrouting",
    url: "{language}/{customnode}/{partial}/{action}",                
    defaults: new { action = "index" },
    parameters: routingParameters);



public class MyCustomSegment : SegmentBase
    public MyCustomSegment(string name)
        : base(name)

    public override string GetVirtualPathSegment(
        System.Web.Routing.RequestContext requestContext,
        System.Web.Routing.RouteValueDictionary values)
           return null;

    public override bool RouteDataMatch(SegmentContext context)

        var segmentPair = context.GetNextValue(context.RemainingPath);
        if (segmentPair.Next.Equals("markets"))
            string path = context.RemainingPath.ToString();
            context.RemainingPath = segmentPair.Remaining;
            UrlBuilder urlBuilder = new UrlBuilder(url);
            object pageReference;
            Global.UrlRewriteProvider.ConvertToInternal(urlBuilder, out pageReference);
            context.RoutedContentLink = pageReference;
            return true;
        return false;


Jul 16, 2013 13:43

I need to do the same thing. Did you have any luck?

May 29, 2015 15:51

Hi guys 

Have you taken a look at Magnus' post on Routing context aware links in Commerce catalog? It was written for EPiServer commerce catalog routing but may point you in the right direction on how to set up custom routes for your needs.


May 29, 2015 18:46

Was a couple of years ago now, i think i gave up, sorry

May 29, 2015 21:46

Lol Jeremy, didn't spot the date. Hope it can help Jesse instead ;)

May 29, 2015 22:00
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.