asp.netcachingroutesroutetable

Removing Specific Routes from ASP.NET 4 RoutesTable


my site has many many routes. since the users can add or remove pages at will, i need a way to reregister the routes from time to time

at reregister i do not want to clear the whole route cache ("RouteTable.Routes.Clear"), but i would rather go thru the table route by route, and using a certain regex on its name, decide if to remove it or not.

after that i will reregister the specific pages that i need to

heres my code meanwhile

        For Each r In RouteTable.Routes
            If CType(r, Route).DataTokens("ConfigID") = ConfigID Then RouteTable.Routes.Remove(r)
        Next

after the first remove it throws an error "Collection was modified; enumeration operation may not execute."

thank you very much for your help!!


Solution

  • It's not possible to get the route name of the route because the name is not a property of the Route. When adding routes to the RouteTable, the name is used as an internal index for the route and it's never exposed.

    There's one way to do this.

    1. When you register a route, set a DataToken on the route with the route name and use that to filter routes.

    The easiest way to do #1 is to probably write your own extension methods for mapping routes.