1

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.

Satpal
  • 132,252
  • 13
  • 159
  • 168
ldp
  • 23
  • 4
  • [Creating custom route constraints in ASP.NET MVC](http://www.prideparrot.com/blog/archive/2012/3/creating_custom_route_constraints_in_asp_net_mvc) blog will help you – Satpal Dec 17 '13 at 18:54

1 Answers1

0

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$"
Ant P
  • 24,820
  • 5
  • 68
  • 105