I have the following routes set up:
context.MapRoute(
"content_article",
"A{rk}/{title}",
new { controller = "Server", action = "Article" },
new { rk = @"^[\dA-Z]{3}$" }
);
context.MapRoute(
"content_contentBlock",
"C{rk}/{title}",
new { controller = "Server", action = "ContentBlock" },
new { rk = @"^[\dA-Z]{3}$" }
);
context.MapRoute(
"content_favoritesList",
"F{rk}/{title}",
new { controller = "Server", action = "FavoritesList" },
new { rk = @"^[\dA-Z]{3}$" }
);
Is there a way that I could combine:
"A{rk}/{title}",
"C{rk}/{title}",
"F{rk}/{title}",
into one single route with an action of Index that’s taken if the URL starts with an A, C or F?
So if I understand correctly you want just one route to cover all three and to point to
Indexaction? If that’s the question than the answer isSure you can
You just have to change your regular expression a little to this route definition:
And that’s it. Parsing of the first letter out is then done by Index action that would likely (and hopefully) execute an appropriate separate method.
As you can see the three methods have the same signature as if they were controller actions, but they’re not, since they’re
private. You can either set them asprivateor decorathe them withNonActionAttributeso they can’t get picked up by your default route, that you likely have as well…