setting lookup values for the url paramater in routes.maproute

76 views Asked by At

Is there an easy way to define lookup values in your route.maproute?

example route:

 routes.MapRoute(
              name: "example route", 
               url: "product/{subcat}", 
          defaults: new { controller = "Redirect", action = "ProcessPage"}
      );

Where: {subcat} can only be the following values: subcat1, subcat2, subcat3

I'm trying to write redirects from an old site to a new site (can't use IIS), the old site does not have consistent urls and some of them fall into patterns of my new site. I would like to avoid writing a route for every single redirect.

1

There are 1 answers

1
Ant P On BEST ANSWER

You can use regular expressions to constrain your route values:

routes.MapRoute(
    name: "example route", 
    url: "product/{subcat}", 
    defaults: new { controller = "Redirect", action = "ProcessPage"},
    constraints: new { subcat = @"^subcat\d*$ }
);

I've assumed here that the constraint is "subcat" appended with an integer (that's the best I can extract from your question) - modify this to meet your requirements. If you need an explicit set of values, you can just use the regex OR operator with your list of accepted strings:

@"^subcat1|subcat2|subcat3$"