1

I am using a custom route handler for a webforms application. I am using routes to determine localization. ie: if the url has es or fr in the route it will load either spanish or french resources.

for example:

www.someroute/es/checkstuff/checkstuff.aspx

will load:

www.someroute/checkstuff/checkstuff.aspx with the spanish resources.

I am configuring the custom routes in global.asax via:

protected void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        foreach (var value in _customRoutes)
        {
            routes.Add(value.RouteName, new Route(value.Route, new CustomRouteHandler(value.ResolvedRoute)));
        }
    }

where _customroutes is a list of routes.

Is there a way to do this with some kind of pattern matching so I can avoid adding a specific route for each page in the application. While I know I could use a t4 template to generate the routes, I guess I am looking for a dynamic way to create the list

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
jparram
  • 804
  • 8
  • 24

2 Answers2

2

I discovered that it was simpler to use MapPageroute than route.Add. With MapPageRoute I was able to use wildcards and with two entries:

        routes.MapPageRoute("Spanish", "es/{*page}", "~/{page}");
        routes.MapPageRoute("Kreyol", "fr/{*page}", "~/{page}");

I was able to provide the required routing for Spanish and Kreole pages.

Thanks to all for your help.

jparram
  • 804
  • 8
  • 24
0

I would handle the language-part through some plain old Rewrite with an HttpModule in the BeginReguest handler and let the Routing engine take care of the rest.

Remember that the Routing mechanism takes place far later than BeginRequest so you can safely determine the language, set the CultureInfo on your thread and rewrite the request url to no contain the language-part, and your Routing would never know about it.

Pauli Østerø
  • 6,878
  • 2
  • 31
  • 48