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.

有帮助吗?

解决方案

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$"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top