How do I get Route name from RouteData?

前端 未结 10 880
猫巷女王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:30

    You can add every route parameters and its not necessary this parameters be in your Url: You can put your route name as a parameter like this inGlobal.asax:

     routes.MapPageRoute("Page",
                    "Page-{ID}",
                   "~/Item_show.aspx", false, new RouteValueDictionary{ { "RouteName" , "Page" }});
    

    And Access it in your page:

    if (RouteData.Values["RouteName"] != null)
               {
                   if (RouteData.Values["RouteName"].ToString() == "Page")
                   {
                       Response.Write(RouteData.Values["RouteName"]);
    
                   }  
    
               }
    

    Best way is not hard way.

提交回复
热议问题