Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do Basecamp-style accounts in Asp.Net Mvc?

For an Asp.Net software as a service application, I want to do account based subdomains like Basecamp and the rest of the 37Signals products have. E.g. acme.myapp.com will load the account for that customer and pull back only their information.

This is easy to do in Ruby on Rails, but how would you handle this functionality in ASP.NET MVC and be able to scale to possibly hundreds of accounts?

like image 756
Wayne Molina Avatar asked Oct 15 '22 13:10

Wayne Molina


2 Answers

Maarten Balliauw's blog covered one method extending RouteBase. I think I've also seen a custom route handler used for this.

Also, this StackOverflow question covered the same question, using a more simplistic approach.

I definitely recommend factoring this code out into the routing side rather than embedding the logic to get domain information in your controllers.

like image 55
JasonTrue Avatar answered Oct 19 '22 03:10

JasonTrue


We use:

public static string GetSubDomain()
        {
            string subDomain = String.Empty;

            if (HttpContext.Current.Request.Url.HostNameType == UriHostNameType.Dns)
            {
                subDomain = Regex.Replace(HttpContext.Current.Request.Url.Host, "((.*)(\\..*){2})|(.*)", "$2");
            }

            if (subDomain.Length == 0)
            {
                subDomain = "www";
            }

            return subDomain.Trim().ToLower();
        }
like image 33
Mark Redman Avatar answered Oct 19 '22 02:10

Mark Redman