How do I get Route name from RouteData?

前端 未结 10 906
猫巷女王i
猫巷女王i 2021-02-05 02:03

I have several routes defined in my Global.asax;

When I\'m on a page I need to figure out what is the route name of the current route, because route name drives my site

10条回答
  •  暖寄归人
    2021-02-05 02:43

    Here's an implementation of @haacked's suggestion - with also a simple 'razor' table to display route data.

    Note: You may not have realized that all the standard 'MapRoute' methods are actually extension methods. Therefore we cannot use the same name. I've just called it 'MapRoute2', because right now thats all I can think of.

    You must replace all calls to MapRoute with a call to MapRoute2, don't forget all AreaRegistration files as well as global.asax.cs

    Extension method:

    public static class RouteNameExtensions
    {
        // RouteCollection
        public static Route MapRoute2(this RouteCollection routes, string name, string url)
        {
            return AddRouteNameDataToken(name, routes.MapRoute(name, url));
        }
    
        public static Route MapRoute2(this RouteCollection routes, string name, string url, object defaults)
        {
            return AddRouteNameDataToken(name, routes.MapRoute(name, url, defaults));
        }
    
        public static Route MapRoute2(this RouteCollection routes, string name, string url, string[] namespaces)
        {
            return AddRouteNameDataToken(name, routes.MapRoute(name, url, namespaces));
        }
    
        public static Route MapRoute2(this RouteCollection routes, string name, string url, object defaults, object constraints)
        {
            return AddRouteNameDataToken(name, routes.MapRoute(name, url, defaults, constraints));
        }
    
        public static Route MapRoute2(this RouteCollection routes, string name, string url, object defaults, string[] namespaces)
        {
            return AddRouteNameDataToken(name, routes.MapRoute(name, url, defaults, namespaces));
        }
    
        public static Route MapRoute2(this RouteCollection routes, string name, string url, object defaults, object constraints, string[] namespaces)
        {
            return AddRouteNameDataToken(name, routes.MapRoute(name, url, defaults, constraints, namespaces));
        }
    
        // AreaRegistrationContext
        public static Route MapRoute2(this AreaRegistrationContext routes, string name, string url)
        {
            return AddRouteNameDataToken(name, routes.MapRoute(name, url));
        }
    
        public static Route MapRoute2(this AreaRegistrationContext routes, string name, string url, object defaults)
        {
            return AddRouteNameDataToken(name, routes.MapRoute(name, url, defaults));
        }
    
        public static Route MapRoute2(this AreaRegistrationContext routes, string name, string url, string[] namespaces)
        {
            return AddRouteNameDataToken(name, routes.MapRoute(name, url, namespaces));
        }
    
        public static Route MapRoute2(this AreaRegistrationContext routes, string name, string url, object defaults, object constraints)
        {
            return AddRouteNameDataToken(name, routes.MapRoute(name, url, defaults, constraints));
        }
    
        public static Route MapRoute2(this AreaRegistrationContext routes, string name, string url, object defaults, string[] namespaces)
        {
            return AddRouteNameDataToken(name, routes.MapRoute(name, url, defaults, namespaces));
        }
    
        public static Route MapRoute2(this AreaRegistrationContext routes, string name, string url, object defaults, object constraints, string[] namespaces)
        {
            return AddRouteNameDataToken(name, routes.MapRoute(name, url, defaults, constraints, namespaces));
        }
    
        private static Route AddRouteNameDataToken(string name, Route route)
        {
            route.DataTokens["route-name"] = name;
            return route;
        }
    }
    

    Here's a simple razor .cshtml file I'm using to display routing information:

    
    
        @foreach (var route in ViewContext.RouteData.Values)
        {
            
        }
        @foreach (var route in ViewContext.RouteData.DataTokens.Where(x=>x.Key != "route-name"))
        {
            
        }
    
    
    Route Key Value
    Route name @ViewContext.RouteData.DataTokens["route-name"]
    - @route.Key @(route.Value ?? "")
    @route.Key @(route.Value ?? "")

提交回复
热议问题