November Happy Hour will be moved to Thursday December 5th.

Remove Trailing Slash

Vote:
 

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?

#198964
Nov 09, 2018 20:05
Vote:
 

Hi,

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.

/Jake

#198969
Nov 09, 2018 21:29
Vote:
 

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

#198970
Edited, Nov 09, 2018 21:42
Vote:
 

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

#198971
Nov 09, 2018 21:53
Vote:
 

I set it in an Initialization Module:

/// <summary>
    /// Set up custom routes
    /// </summary>
    [InitializableModule]
    [ModuleDependency(typeof(ServiceContainerInitialization))]
    public class RoutingInitialization : IInitializableModule
    {
        private static bool _initialized;

        public void Initialize(InitializationEngine context)
        {
            if (_initialized || context.HostType != HostType.WebApplication)
                return;
            
            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)
        {
            
        }
#198972
Nov 09, 2018 22:00
Vote:
 

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.

#198973
Nov 09, 2018 22:26
Vote:
 

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

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