First I found this post that describes how to create a DomainRoute class that will allow you to include the domain in the route. Pretty cool. The next step was to create some methods that allow me to include the domain when mapping a route in the area registration. I created two extension methods for that.
10 public static class DomainRouteExtensions
11 {
12 public static Route MapRoute(this RouteCollection routes, string name, string domain, string url, object defaults, object constraints, string[] namespaces)
13 {
14 if (routes == null)
15 {
16 throw new ArgumentNullException("routes");
17 }
18 if (url == null)
19 {
20 throw new ArgumentNullException("url");
21 }
22
23 DomainRoute route = new DomainRoute(domain, url, defaults, new MvcRouteHandler())
24 {
25 Defaults = new RouteValueDictionary(defaults),
26 Constraints = new RouteValueDictionary(constraints),
27 DataTokens = new RouteValueDictionary()
28 };
29
30 if ((namespaces != null) && (namespaces.Length > 0))
31 {
32 route.DataTokens["Namespaces"] = namespaces;
33 }
34
35 routes.Add(name, route);
36
37 return route;
38 }
39
40 public static Route MapRoute(this AreaRegistrationContext context, string name, string domain, string url, object defaults, object constraints, string[] namespaces)
41 {
42 if (namespaces == null && context.Namespaces != null)
43 {
44 namespaces = context.Namespaces.ToArray();
45 }
46
47 Route route = context.Routes.MapRoute(name, domain, url, defaults, constraints, namespaces);
48 route.DataTokens["area"] = context.AreaName;
49
50 // disabling the namespace lookup fallback mechanism keeps this areas from accidentally picking up
51 // controllers belonging to other areas
52 bool useNamespaceFallback = (namespaces == null || namespaces.Length == 0);
53 route.DataTokens["UseNamespaceFallback"] = useNamespaceFallback;
54
55 return route;
56 }
57 }
Basically, these two extension methods are just slight modifications to the existing MVC methods that allow me to include a domain string and replace the standard Route object with a DomainRoute object. After a little debuging, this worked well with one small change to the DomainRoute.GetRouteData method by adding the DataTokens to the returned data object
1 foreach (var item in this.DataTokens)
2 {
3 data.DataTokens.Add(item.Key, item.Value);
4 }
5 }
6
7 return data;
Now that that is all done, all that is left is to add a route in the area registration.
19 public override void RegisterArea(AreaRegistrationContext context)
20 {
21 context.MapRoute(
22 "KMI_Default",
23 "test.kmi.com",
24 "{controller}/{action}/{id}",
25 new { controller = "Home", action = "Index", id = UrlParameter.Optional },
26 null,
27 null);
28 }
And thats it. The two nulls are needed to make sure the correct overload is called and the domain string is not mapped to the constraints since it is of type object. Do this will a couple more areas and multiple domains mapped to the same root folder in IIS can route to different areas in the MVC application. A little more work needs to be done to make it a complete solution. As it stands right now, the DomainRoute class does not support constraints.