Currently I have a scenario where I have to have more than one site and multiple domains/subdomains in a single Windows Azure web role.
For obvious reasons Windows Azure give us FQDN (like myapp.cloudapp.net) instead of IP address so we have to use CNAME to point to our instance/s. The only problem with this approach is that only domain name cannot have CNAME record. For instance www.mydomain.org is fine, but just mydomain.org is not since it needs specific IP address. For the time being this was solved by pointing it to some specific address which would redirect the user to www of that domain.
That part was simple and straightforward, but now we need something inside our web role, that would detect requested domain and serve appropriate content. There are a couple of threads online that deal with the issue of addressing multi-tenant application in Windows Azure and most of them deal with url rewriting on the single domain. You can read more about it here and here.
In my project I used MVC Areas to separate different sites so all I needed was MVC routing to use domain name from incoming request in its routing configuration. I found an excellent article on the subject here, but unfortunately it was written before MVC 2 introduced areas, so in order to use it lets add area support to it.
First lets download the sample here, open and if needed convert solution and open DomainRoute.cs. We only need to add one line to the end of GetRouteData method:
if (Defaults.Keys.Contains("area"))
data.DataTokens.Add("area", Defaults["area"]);
so that it looks like:
if (DataTokens != null)
{
foreach (var token in DataTokens)
{
data.DataTokens.Add(token.Key, token.Value);
}
}
if (Defaults.Keys.Contains("area"))
data.DataTokens.Add("area", Defaults["area"]);
}
return data;
}
And we are ready to register our areas as their own domains/subdomains
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.Add("subodomain_default", new DomainRoute(
"subdomain.mydomain.si",
"{controller}/{action}/{id}",
new { area = AreaName, controller = "Home", action = "Index",
id = UrlParameter.Optional }
)
{
DataTokens = new RouteValueDictionary(new {
Namespaces = new string[] { "MvcWebRole1.Areas.subdomain.Controllers" } })
}
);
}
I strongly suggest you pass Namspaces to every route registration so you can have multiple controllers with the same name serving different tenants. Soon Ill add a DomainRouteExtension and post it here so the usage will be even simpler.
ccbf111c-202f-4942-897a-3513e037139d|26|5.0