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

冷暖自知 提交于 2019-12-04 09:57:09

问题


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?


回答1:


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();
        }



回答2:


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.




回答3:


It's not very different compared to RoR. Just get the HTTP-Request, take the Host-Value, split it (at each dot) and take the first part to get the subdomain.

string subdomain = requestContext.HttpContext.
                      Request.Headers["Host"].Split('.')[0];

Then just resolve the subdomain to the Companies account.



来源:https://stackoverflow.com/questions/1386327/how-to-do-basecamp-style-accounts-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!