How can I get the route name in controller in ASP.NET MVC?

前端 未结 6 2021
我在风中等你
我在风中等你 2020-11-28 22:45

ASP.NET MVC routes have names when mapped:

routes.MapRoute(
    \"Debug\", // Route name -- how can I use this later????
    \"debug/{controller}/{action}/{i         


        
相关标签:
6条回答
  • 2020-11-28 22:49

    another option - use MapRoute with string[] namespaces argument, then you can see your namespaces as RouteData.DataTokens["Namespaces"]

    0 讨论(0)
  • 2020-11-28 22:51

    This does not directly answer the question (if you want to be pedantic); however, the real objective seems to be to get a route's base URL, given a route name. So, this is how I did it:

    My route was defined in RouteConfig.cs as:

    routes.MapRoute(
                name: "MyRoute",
                url: "Cont/Act/{blabla}",
                defaults: new { controller = "Cont", action = "Act"}
            );
    

    And to get the route's base URL:

    var myRoute = Url.RouteUrl("MyRoute", new { blabla = "blabla" }).Replace("blabla", "");
    

    It gave me the route's base URL that I wanted:

    /Cont/Act/
    

    Hope this helps.

    0 讨论(0)
  • 2020-11-28 23:08

    The route name is not stored in the route unfortunately. It is just used internally in MVC as a key in a collection. I think this is something you can still use when creating links with HtmlHelper.RouteLink for example (maybe somewhere else too, no idea).

    Anyway, I needed that too and here is what I did:

    public static class RouteCollectionExtensions
    {
        public static Route MapRouteWithName(this RouteCollection routes,
        string name, string url, object defaults, object constraints)
        {
            Route route = routes.MapRoute(name, url, defaults, constraints);
            route.DataTokens = new RouteValueDictionary();
            route.DataTokens.Add("RouteName", name);
    
            return route;
        }
    }
    

    So I could register a route like this:

    routes.MapRouteWithName(
        "myRouteName",
        "{controller}/{action}/{username}",
        new { controller = "Home", action = "List" }
        );
    

    In my Controller action, I can access the route name with:

    RouteData.DataTokens["RouteName"]
    

    Hope that helps.

    0 讨论(0)
  • You could pass route name through route values using default value of additional parameter:

    routes.MapRoute(
        name: "MyRoute",
        url: "{controller}/{action}/{id}",
        defaults: new { routeName = "MyRoute", controller = "Home", action = "Index", id=UrlParameter.Optional }
    );
    

    Then, it is possible to get passed value from controller context:

    string routeName = ControllerContext.RouteData.Values["routeName"].ToString();
    
    0 讨论(0)
  • 2020-11-28 23:11

    If using the standard MapRoute setting like below:

    routes.MapRoute(    name: "Default",
                        url: "{controller}/{action}/{id}",
                        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    

    ...this will work in the view...

    var routeName = Url.RequestContext.RouteData.Values["action"].ToString();
    
    0 讨论(0)
  • 2020-11-28 23:16

    An alternative solution could be to use solution configurations:

    protected override OnActionExecuting()
    {
        #if DEBUG
    
            // set up stuff in the ViewData
    
        #endif
    
        // continue
    }
    

    There shouldn't really ever be a need to reference the route name like this - which I suppose is why MVC makes it so difficult to do this sort of thing.

    0 讨论(0)
提交回复
热议问题