简体   繁体   English

ASP.NET MVC。 数据驱动的子域名?

[英]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, 我考虑过的一些选择,

  1. OnActionExecuting in the controller OnActionExecuting在控制器中
  2. In a custom action filter 在自定义操作筛选器中
  3. IIS module IIS模块
  4. As part of the routing setup - a custom route class that knows about the valid sub-domains - similar to this approach - http://blog.maartenballiauw.be/post/2009/05/20/ASPNET-MVC-Domain-Routing.aspx 作为路由设置的一部分 - 一个知道有效子域的自定义路由类 - 与此方法类似 - http://blog.maartenballiauw.be/post/2009/05/20/ASPNET-MVC-Domain-Routing的.aspx

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.

相关问题 ASP.NET MVC。 热将数据从视图传输到控制器而不会丢失数据 - Asp.Net MVC. Hot to transmit data from view to controller without dataloss ASP.Net MVC。 如何始终在Entity Framework查询中包含相关数据? - ASP.Net MVC. How always include related data in Entity Framework queries? ASP.NET MVC中的复选框列表。 我无法将数据检索到IEnumerable或List - Checkbox List in ASP.NET MVC. I cant retrieve data to a IEnumerable or List ASP.NET MVC中的全局错误处理。可能? - Global error handling in ASP.NET MVC. Possible? ASP.NET C#MVC。 运行已编译的应用程序 - Asp.Net C# mvc. Running compiled application ASP.NET MVC。 如何在部分中插入SelectList? - ASP.NET MVC. How insert a SelectList inside an Partial? ASP.NET MVC。 PagedList通过多个参数过滤 - ASP.NET MVC. PagedList filtering by multiple params ASP.NET MVC。 Autofac和多个连接字符串 - ASP.NET MVC. Autofac and multiple connection strings ASP.Net MVC允许URL中的子域 - ASP.Net MVC Allow Subdomains in URL asp.net mvc。 通过Get(表单)发送数据并在标签中返回成功?(如何将数据发送和返回到同一视图) - asp.net mvc. Sending data through Get (form) and returning success in a label?(how to send and return data to the same view)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM