Can you change the default content routes in Episerver?


By default - EPiserver maps content using a route /{lang}/{node}{action} - with {lang} being the optional language segment, {node} being the content url hierarchy calculated from the page tree and {action} being the controller action which has a default of "Index"

Can you modify the order of these?

Ideally I'd like a route structure of  


Is this possible? (I think it probably is as I imagine I can add or remove entries from the routetable). Would this have any other impact?

#190738 Apr 16, 2018 16:34
  • Johan Björnfot
    Member since: 2004

    The segment handler for {language} is responsible for setting the language on the routing context. The handler for {node} segment will use the language specified on context to determine which language version of content the request is for. So if you change the order it will be sligtly different in meaning the {node} segment has no language specified on context so it will try to load from segments and then will the language be set later.

    In theory it should work to swap the order of the segments (I tried and it seemed to work in practice as well) but it is nothing we officially support (since it is not part of our test matrix). But with that said you could try your self. You could either do it in your Global file (as in code example below) or in an InitializableModule:

      protected override void RegisterRoutes(RouteCollection routes)
                //Find index of default route
                var index = -1;
                for (int i=0; i < routes.Count; i++)
                    if (routes[i] is IContentRoute contentRoute && contentRoute.Name.Equals("pages"))
                        index = i;
                //remove default route
                //register route with different segment order
                   name: "pages",
                   url: "{node}/{language}/{partial}/{action}",
                   defaults: new { action = "index" });
                //new route is registered last move to original place
                var newRoute = routes.Last();
                routes.RemoveAt(routes.Count - 1);
                routes.Insert(index, newRoute);
    #191165 Apr 23, 2018 9:49