Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC. Data driven subdomains?

Tags:

c#

asp.net-mvc

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 - i.e. 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 (i.e. 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 ?? i.e. 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.

like image 927
Matt Randle Avatar asked Nov 09 '11 09:11

Matt Randle


1 Answers

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.

like image 64
Jakub Konecki Avatar answered Oct 11 '22 10:10

Jakub Konecki