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

前端 未结 6 2037
我在风中等你
我在风中等你 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: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.

提交回复
热议问题