如何在Asp.Net Mvc中做Basecamp风格的账户?

发布于 2024-08-04 10:34:31 字数 178 浏览 8 评论 0原文

对于 Asp.Net 软件即服务应用程序,我想要执行基于帐户的子域,例如 Basecamp 和其他 37Signals 产品。例如,acme.myapp.com 将加载该客户的帐户并仅提取他们的信息。

这在 Ruby on Rails 中很容易做到,但是如何在 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无力看清 2024-08-11 10:34:32

Maarten Balliauw 的博客介绍了一种方法扩展 RouteBase。我想我还看到了用于此目的的自定义路由处理程序。

另外,这个 StackOverflow Question 使用更简单的方法涵盖了同一问题。

我绝对建议将此代码分解到路由端,而不是嵌入逻辑以在控制器中获取域信息。

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.

半世晨晓 2024-08-11 10:34:32

我们使用:

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

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();
        }
单身狗的梦 2024-08-11 10:34:32

与RoR相比并没有太大区别。只需获取 HTTP 请求,获取主机值,将其拆分(在每个点处)并获取第一部分即可获取子域。

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

然后只需将子域解析为公司帐户即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文