routing - removing a segment from the url

Vote:
 

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 http://joelabrahamsson.com/custom-routing-for-episerver-content/ http://world.episerver.com/Modules/Forum/Pages/Thread.aspx?id=68317 http://world.episerver.com/Documentation/Items/Developers-Guide/EPiServer-CMS/7/Routing/Routing/ http://world.episerver.com/Modules/Forum/Pages/Thread.aspx?id=65422

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

many thanks

 

 

 

 

 

 

 

 

 

#73326
Jul 15, 2013 19:57
Vote:
 

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)
{
    if(e.UrlBuilder!=null)
    {
        if(e.UrlBuilder.Path.StartsWith("markets/"))
        {
          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);

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

    base.RegisterRoutes(routes);


}

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;
    }

}

#73366
Jul 16, 2013 13:43
Vote:
 

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

#122301
May 29, 2015 15:51
Vote:
 

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.

David

#122313
May 29, 2015 18:46
Vote:
 

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

#122316
May 29, 2015 21:46
Vote:
 

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

#122317
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.