views:

366

answers:

2

I am fairly new to asp.net, and have little experience with iis. I would like to have each user of my application get their own sub-domain, but all use the same controllers. The subdomain would then control what content is displayed.

Example:

user1subdomain.mydomain.com/Whatever
user2subdomain.mydomain.com/Whatever

Will both use the same controller. Ideally a parameter could give the user name to the controller, which could then display the appropriate content. I would like it to be flexible enough that new subdomains could be added to the database without rewriting routing rules every time a new subdomain is added.

+1  A: 

Mostly not a problem. I think!

In terms of the application/routing the routing starts where the domain ends so mapping multiple domains to the same application is not a problem, that will just work.

In terms of IIS you can map as many domains as you want (well there's bound to be a limit) to a single site - I'm not sure if you can use a wildcard - what version of IIS are you using?

When a request arrives there are events you can hook to look at the domain and hence set up parameters you want (user for example), the root URL for the request is available from the context later in the cycle too - but you'll want to pick it up early.

If you can do wildcards it becomes fairly trivial - pick up the request, validate the subdomain against the users in the database (if not valid redirect to the default site), set the user and carry on through the normal routing.

If you can't do wildcards then the challenge is adding host headers to the IIS application (website) on the fly from your application as users are added to the database.

Murph
With IIS if a Host Header Value isn't specified for a site then it accepts all host header values, you just have to ensure that you only have 1 site per IP setup in IIS in this manner.
CptSkippy
+1  A: 

MVC is not bound to the domain, just to the path (e.g. http://domain/path).

To do this properly you need the following...

  1. Wildcard DNS setup for *.yourdomain.com pointing to your server.
  2. The site in IIS setup with no Host Header. Any other sites hosted in that instance of IIS on the same IP must have Host headers specified.
  3. Your application will need to check the request host header either on page load, session start or some other event.
CptSkippy