How do I get Route name from RouteData?

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

    For C# you can declare your routes like so:

            routeCollection.MapPageRoute("RouteForProduct", "Product/{ProductName}", "~/IRShop.aspx", false, new RouteValueDictionary { { "Section", "product" } });
            routeCollection.MapPageRoute("RouteForProductList", "ProductList/{CatName}", "~/IRShop.aspx", false, new RouteValueDictionary { { "Section", "productlist" } });
            routeCollection.MapPageRoute("RouteForContentList", "Content/{PageName}", "~/IRShop.aspx", false, new RouteValueDictionary { { "Section", "content" } });
    

    Then in your method where you need the route you can then call the following:

    var x = Page.RouteData.Values["Section"].ToString();
    

    And you will have a string set in your global.asax to then use as you need.

提交回复
热议问题