An outlet for my obsession with technology
ASP.NET MVC Routing domain.com/*
Scenario; you have to pass wildcard requests to a controller for processing. Think myspace.com style vanity names.
Solution: Not as nice as .htaccess but it works; and I think I’m doing it write.
Step 1: Add Routes to Global.asa
1: routes.MapRoute(
2: "Default",
3: "{controller}/{action}/{id}",
4: new { controller = "Home", action = "Index", id = "" },
5: new { controller = new Constraints("Account", "User", "Home") }
6: );
7:
8: routes.MapRoute(
9: "User",
10: "{id}",
11: new { controller = "User", action = "Index", id = "" }
12: );
Step 2: Create Class that implements IRouteConstraint
1: public class Constraints : IRouteConstraint
2: {
3: private string[] _matches = null;
4:
5: public Constraints(params string[] matches)
6: {
7: _matches = matches;
8: }
9:
10: public bool Match(HttpContextBase httpContext,
11: Route route,
12: string parameterName,
13: RouteValueDictionary values,
14: RouteDirection routeDirection)
15: {
16: bool foundMatch = false;
17: foreach (string match in _matches)
18: {
19: if (String.Compare(values[parameterName].ToString(), match, true)
20: == 0)
21: {
22: foundMatch = true;
23: }
24:
25: }
26: return foundMatch;//not matching!
27: }
28: }
No related posts.
| Print article | This entry was posted by Eric Polerecky on July 30, 2009 at 1:30 pm, and is filed under ASP.NET MVC. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |