Reputation: 32798
I have the following route where any URL that starts with A, F or L is directed to the Index action. It looks like a C sharp regular expression is used.
context.MapRoute(
"content",
"{page}/{title}",
new { controller = "Server", action = "Index" },
new { page = @"^[AFL][0-9A-Z]{3}$" }
);
I would like to do something similar but this time direct any URL that has an action of Menus, Objectives, Pages or Topics to go to the Index action and pass the words "Menus", "Objectives", "Pages" or "Topics" to the Index action as a parameter:
Can someone show me how I can do this. It looks like a C# kind of regular expression bubut I am not sure how to do the expression I need for the second route.
Upvotes: 0
Views: 168
Reputation: 6882
Look at this example here...
http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-route-constraint-cs you have define a route with a parameter.
routes.MapRoute(
"Content",
"Content/{TypeOfAction}",
new {controller="Content", action="Index"},
new {@"\b(Menus|Objectives|Pages|Topics)\b"}
);
Asuming you have a ContentController with an Index action which handles TypeOfAction as parameter
Edited the answer:
\b
in a Regex finds word boundaries... have not tested it but should work...
http://www.regular-expressions.info/wordboundaries.html
http://www.regular-expressions.info/alternation.html
Upvotes: 0
Reputation: 6832
All you need is a simple route constraint :
context.MapRoute(
"content",
"{page}/{title}",
new { controller = "Server", action = "Index" },
new { page = @"Menus|Objectives|Pages|Topics" }
);
And then your action method signature will look like this :
public ActionResult Index(string page)
{
...
return View();
}
Upvotes: 1