简体   繁体   中英

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).

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.

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)

Some options I've considered,

  1. OnActionExecuting in the controller
  2. In a custom action filter
  3. IIS module
  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

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.

I would create a custom action filter and registered it globally in Global.asax (no worries when adding new controllers).

You can also consider creating a custom MvcHandler and specify it when declaring routes. 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.

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.

            var dotIndex = HostingEnvironment.SiteName.IndexOf('.');
            if (dotIndex > 0)
            {
                var subdomain = HostingEnvironment.SiteName.Substring(0, dotIndex);
                customerCode = subdomain;
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM