simple way to constrain a route to only allow enum values
Say I have the following enum:
public enum SearchType { Facilities, Courses, Exercises};
And I only want those the be available in route
Search/{SearchType}. I could constrain it manually with a string, but then what if the enum changes and I have this in a bunch of routes? Here is a simple way to accomplish this constraint:
public static void RegisterRoutes(RouteCollection routes)
{
List searchTypeL = new List();
foreach (Enums.SearchType type in
Enum.GetValues(typeof(Enums.SearchType)))
{ searchTypeL.Add(type.ToString()); }
string searchTypes = string.Join("|", searchTypeL);
routes.MapRoute(
"Searches",
"Search/{type}",
new { controller = "Search", action = "Index" },
new { type = searchTypes }
)
...
Very basic, all this does is enumerate through your enum and create a simple string to filter with.