[英]ASP.NET MVC. Data driven subdomains?
We are creating a multi-tennant web application where we identify tenants via a subdomain (customer1.ourapp.com, customer2.ourapp.com, etc). 我们正在创建一个多租户Web应用程序,我们通过子域(customer1.ourapp.com,customer2.ourapp.com等)识别租户。
Setup of subdomains has to be data driven - ie we don't want to have to modify IIS config (manually or programmatically) every time we get a new customer. 子域的设置必须是数据驱动的 - 即我们不希望每次获得新客户时都必须(手动或以编程方式)修改IIS配置。
In MVC where is the best place to check that a subdomain in a request is valid (ie the subdomain exists in some table in the database) 在MVC中,检查请求中的子域是否有效的最佳位置(即子域存在于数据库的某个表中)
Some options I've considered, 我考虑过的一些选择,
I think that conceptually this is a routing task so the last option seems right ?? 我认为从概念上讲这是一个路由任务,所以最后一个选项似乎正确? ie a request with a subdomain that doesn't exist is essentially an invalid url so it shouldn't match against a route and should instead fall through to a 404. This would also allow us to explicitly define routes that do or don't require a valid subdomain.
即,具有不存在的子域的请求本质上是一个无效的URL,因此它不应与路由匹配,而应该转到404.这也允许我们明确定义需要或不需要的路由一个有效的子域名。
I would create a custom action filter and registered it globally in Global.asax (no worries when adding new controllers). 我将创建一个自定义操作过滤器并在Global.asax中全局注册它(添加新控制器时无需担心)。
You can also consider creating a custom MvcHandler
and specify it when declaring routes. 您还可以考虑创建自定义
MvcHandler
并在声明路由时指定它。 This will allow you to specify a few routes (ie. for static content), which can be shared between all clients. 这将允许您指定几个路由(即静态内容),可以在所有客户端之间共享。
Other solution is to use only routing and stick to the single domain, so you don't have to shell out for the expensive SSL certificate for wildcard domain. 其他解决方案是仅使用路由并坚持使用单个域,因此您不必为通配符域支付昂贵的SSL证书。
I was doing it like this in my Base Controller Class before, however, like @Jakub said, using subdomain will be expensive if you or your client need SSL certificate thereafter. 我以前在我的基本控制器类中这样做,但是,像@Jakub所说,如果您或您的客户端此后需要SSL证书,使用子域将会很昂贵。
var dotIndex = HostingEnvironment.SiteName.IndexOf('.');
if (dotIndex > 0)
{
var subdomain = HostingEnvironment.SiteName.Substring(0, dotIndex);
customerCode = subdomain;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.