SaaS CMS has officially launched! Learn more now.

Remove Trailing Slash


I would like to never have the trailing slash in my URLs. I can remove it with redirects, and with routing settings. But I am having trouble coming up with a good way to remove it when I am generating Urls that wiull appear on the page. There seems to be several different ways to accomplish getting Urls on the page, and I need to account for all of them (Url Resolver, Url Extension methods, UrlHelper extension methods, etc...) Additionally, in these cases I seem to need to get rid of it with string replcaement, which just feels dirty. Is there a more centralized way to accomplish this?

Nov 09, 2018 20:05


You need to set UseTrailingSlash.

Something like:

var routingOptions = ServiceLocator.Current.GetInstance<RoutingOptions>();
routingOptions.UseTrailingSlash = false;

in an Initialization Module or Application_Start should work.


Nov 09, 2018 21:29

Yes, I tried that. Unfortunately, it didnt fix the problem.

Edited, Nov 09, 2018 21:42

Where did you set it? Are you able to give a code sample?

Nov 09, 2018 21:53

I set it in an Initialization Module:

/// <summary>
    /// Set up custom routes
    /// </summary>
    public class RoutingInitialization : IInitializableModule
        private static bool _initialized;

        public void Initialize(InitializationEngine context)
            if (_initialized || context.HostType != HostType.WebApplication)
            RouteTable.Routes.LowercaseUrls = true;
            RouteTable.Routes.AppendTrailingSlash = false;

            RouteTable.Routes.MapRoute("Sitemap without path", "sitemap.xml", new { controller = "XmlSitemap", action = "Index" });
            RouteTable.Routes.MapRoute("Sitemap with path", "{path}sitemap.xml", new { controller = "XmlSitemap", action = "Index" });
            RouteTable.Routes.MapRoute("Sitemap with language", "{language}/sitemap.xml", new { controller = "XmlSitemap", action = "Index" });
            RouteTable.Routes.MapRoute("Sitemap with language and path", "{language}/{path}sitemap.xml", new { controller = "XmlSitemap", action = "Index" });
            RouteTable.Routes.MapRoute("Robots", "robots.txt", new { controller = "Robots", action = "Robots" });

            _initialized = true;

        public void Uninitialize(InitializationEngine context)
Nov 09, 2018 22:00

Ok, that's not the same as my example.

You're setting AppendTrailingSlash for the standard ASP.NET MVC routing. Episerver has it's own routing, hence why you need to set UseTrailingSlash in the Episerver RoutingOptions class.

Nov 09, 2018 22:26

Thank you. I knew there had to be a centralized way to do it.

Nov 09, 2018 23:00
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.