tags:

views:

444

answers:

5

I'm looking at using ASP.NET for a new SaaS service, but for the love of me I can't seem to figure out how to do account lookups based on subdomains like most SaaS applications (e.g. 37Signals) do.

For example, if I offer yourname.mysite.com, then how would I use ASP.NET (MVC specifically) to extract the subdomain so I can load the right template (displaying your company's name and the like)? Can it be done with regular routing?

This seems to be a common thing in SaaS so there has to be an easy way to do it in ASP.NET; I know there are plugins that do it for other frameworks like Ruby on Rails.

+2  A: 

You should be able to pick this up from the ServerVariables collection, but first you need to configure IIS and DNS to work correctly. So you know 37Signals probably use Apache or another open source, unix web server. On Apache this is referred to as VirtualHosting.

To do this with IIS you would need to create a new DNS entry (create a CNAME yourname.mysite.com to application.mysite.com) for each domain that points to your application in IIS (application.mysite.com).

You then create a host header entry in the IIS application (application.mysite.com) that will accept the header yourname.mysite.com. Users will actually hit application.mysite,com but the address is the custom subdomain. You then access the ServerVariables collection to get the value to decide on how to customize the site.

Note: there are several alternative implementations you could follow depending on requirements.

  • Handle the host header processing at a hardware load balancer (more likely 37Signals do this, than rely on the web server), and create a custom HTTP header to pass to the web application.
  • Create a new web application and host header for each individual application. This is probably an inefficient implementation for a large number of users, but could offer better isolation and security for some people.
BrianLy
A: 

How can it scale to support a potentially infinite number of accounts? There has to be a way to simulate this programmatically or how can anyone have a successful site? I could have 500 accounts, surely it would be silly to require someone to manually add 500 headers?

Wayne M
A: 

The way I have done it is with HttpContext.Request.ServerVariables["HTTP_HOST"].Split(".").

Let me know if you need more help.

buggs
+3  A: 

This works for me:

    //--------------------------------------------------------------------------------------------------------------------------
    public string GetSubDomain()
    {
        string SubDomain = "";

        if (Request.Url.HostNameType == UriHostNameType.Dns)
            SubDomain = Regex.Replace(Request.Url.Host, "((.*)(\\..*){2})|(.*)", "$2");
        if (SubDomain.Length == 0)
            SubDomain = "www";
        return SubDomain;
    }

I'm assuming that you would like to handle multiple accounts within the same web application rather than building separate sites using the tools in IIS. In our work, we started out creating a new web site for each subdomain but have found that this approach doesn't scale well - especially when you release an update and then have to modify dozens of sites! Thus, I do recommend this approach rather than the server-oriented techniques suggested above based on several years worth of experience doing exactly what you propose.

The code above just makes sure that this is a fully formed URL (rather, say, than an IP address) and returns the subdomain. It has worked well for us in a fairly high-volume environment.

Mark Brittingham
+1  A: 

You need to configure your DNS to support wildcard subdomains. It can be done by adding an A record pointing to your IP address, like this:

* A 1.2.3.4

Once its done, whatever you type before your domain will be sent to your root domain, where you can get by splitting the HTTP_HOST server variable, like the user buggs said above:

 string user = HttpContext.Request.ServerVariables["HTTP_HOST"].Split(".")

//use the user variable to query the database for specific data

PS. If you are using a shared hosting you're probably going to have to by a Unique IP addon from them, since it's mandatory for the wildcard domains to work. If you're using a dedicated hosting you already have your own IP.

holiveira