Routing is a 2-way map. As far as I am aware, extending RouteHandler can only be used to map incoming routes. But for generating URLs (for linking between your pages), this approach won't work.
So, a better option is to inherit RouteBase (or Route) for advanced route customization. You just need to override GetRouteData to map a URL to a dictionary of route values, and override GetVirtualPath to map a dictionary of route values to a URL. By convention, both methods should return null
if they don't match any value (and the routing framework will then attempt the next Route registered).
Then just configure your custom route in your RouteConfig file.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
// Add a custom Route instance.
routes.Add(new MyCustomRoute());
// Add other routes...
}
}
As far as your surrogate key is concerned, there are a few different approaches you could use.
- Cache the key-surrogate key mapping in a Dictionary. Lookup the key from the map and add it to the route values in GetRouteData, and do a reverse lookup key-surrogate key for GetVirtualPath.
- Cache the key-surrogate key mapping in a Dictionary. Lookup the key from the map and do a 301 redirect to the page with the key in the URL. There would not need to be a reverse lookup in this case.
- Use either of the above approaches, but use a different caching strategy. It is possible to use file caching or a distributed cache with System.Runtime.Caching.