How to get a trailing slash appended to page routes?

拟墨画扇 提交于 2019-12-06 05:01:00

I have now found the solution to my original question. I found the following article which describes how you can ensure all of your Url's are in lowercase, so I just used this example code, but appended a trailing slash where it performs the ToLowerInvariant().

So, my 2 helper classes now look like this:

public class LowercaseRoute : System.Web.Routing.Route
{
    public LowercaseRoute(string url, IRouteHandler routeHandler)
        : base(url, routeHandler) { }
    public LowercaseRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler)
        : base(url, defaults, routeHandler) { }
    public LowercaseRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler)
        : base(url, defaults, constraints, routeHandler) { }
    public LowercaseRoute(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.ToLowerInvariant() + "/";

        return path;
    }
}

public static class RouteCollectionExtensions
{
    public static void MapRouteLowercase(this RouteCollection routes, string name, string url, object defaults)
    {
        routes.MapRouteLowercase(name, url, defaults, null);
    }

    public static void MapRouteLowercase(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 LowercaseRoute(url, new MvcRouteHandler())
        {
            Defaults = new RouteValueDictionary(defaults),
            Constraints = new RouteValueDictionary(constraints)
        };

        if (String.IsNullOrEmpty(name))
            routes.Add(route);
        else
            routes.Add(name, route);
    }
}

Within my global.asax, instead of using 'MapRoute' within my RegisterRoutes routine, I call my new MapRouteLowercase method instead (passing in the same parameters), e.g.

routes.MapRouteLowercase("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });

The original article that I got this code from can be found here:

http://goneale.com/2008/12/19/lowercase-route-urls-in-aspnet-mvc/

to make the generated urls always lowercase and with a final trailing slash I add the TidyRouting library to my solution, then I simply replace the "MapRoute" method with the new "MapTidyRoute".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!