7

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.

1
  • 1
    I think you can easily do this with mvc routing .. You will get required info from HERE Commented Nov 9, 2011 at 13:00

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.