Add a trailing slash at the end of each url?

后端 未结 6 658
傲寒
傲寒 2020-12-01 00:15

I have a little problem here. I need to add a trailing slash at the end of each url in the site I\'m working on. I defined all the links inside the site to have a trailing s

6条回答
  •  旧巷少年郎
    2020-12-01 00:45

    You can create a new Route which overrides the GetVirtualPath method. In this method you add a trailing slash to the VirtualPath. Like this:

    public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
    {
         VirtualPathData path = base.GetVirtualPath(requestContext, values);
    
         if (path != null)
             path.VirtualPath = path.VirtualPath + "/";
         return path;
    }
    

    For brevity I posted the whole example on CodePaste.net

    Now all you have to do is register the routes with routes.MapRouteTrailingSlash() instead of routes.MapRoute().

    routes.MapRouteTrailingSlash("register",
                                 "register",
                                 new {controller = "Users", action = "Register"}
    );
    

    The route will then add a slash to the path when the GetVirtualPath() is called. Which RedirectToAction() will do.

    Update: Because the CodePaste link is down, here is the full code:

    public class TrailingSlashRoute : Route {
        public TrailingSlashRoute(string url, IRouteHandler routeHandler)
            : base(url, routeHandler) {}
    
        public TrailingSlashRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler)
            : base(url, defaults, routeHandler) {}
    
        public TrailingSlashRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints,
                              IRouteHandler routeHandler)
            : base(url, defaults, constraints, routeHandler) {}
    
        public TrailingSlashRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints,
                              RouteValueDictionary dataTokens, IRouteHandler routeHandler)
            : base(url, defaults, constraints, dataTokens, routeHandler) {}
    
        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) {
            VirtualPathData path = base.GetVirtualPath(requestContext, values);
    
            if (path != null)
                path.VirtualPath = path.VirtualPath + "/";
            return path;
        }
    }
    
    public static class RouteCollectionExtensions {
        public static void MapRouteTrailingSlash(this RouteCollection routes, string name, string url, object defaults) {
            routes.MapRouteTrailingSlash(name, url, defaults, null);
        }
    
        public static void MapRouteTrailingSlash(this RouteCollection routes, string name, string url, object defaults,
                                             object constraints) {
            if (routes == null)
                throw new ArgumentNullException("routes");
    
            if (url == null)
                throw new ArgumentNullException("url");
    
            var route = new TrailingSlashRoute(url, new MvcRouteHandler())
                        {
                            Defaults = new RouteValueDictionary(defaults),
                            Constraints = new RouteValueDictionary(constraints)
                        };
    
            if (String.IsNullOrEmpty(name))
                routes.Add(route);
            else
                routes.Add(name, route);
        }
    }
    

提交回复
热议问题