I implemented a similar sort of thing for multi-tenanted applications, but using an abstract base Controller rather than a custom Route class. My blog post on it is here.
–
Luke SampsonJul 6 '09 at 6:26

No, you can add a database field called something like "subdomain" that you will be what you're expecting the subdomain to be for a particular user, or whatever else, then just do a lookup on the subdomain.
–
Ryan HayesNov 25 '12 at 23:50

Could anybody recommend a webforms version of this?
–
MatthewTMar 1 '14 at 1:44

To capture the subdomain while retaining the standard MVC5 routing features, use the following SubdomainRoute class derived from Route.

Additionally, SubdomainRoute allows the subdomain optionally to be specified as a query parameter, making sub.example.com/foo/bar and example.com/foo/bar?subdomain=sub equivalent. This allows you to test before the DNS subdomains are configured. The query parameter (when in use) is propagated through new links generated by Url.Action, etc.

The query parameter also enables local debugging with Visual Studio 2013 without having to configure with netsh or run as Administrator. By default, IIS Express only binds to localhost when non-elevated; it won't bind to synonymous hostnames like sub.localtest.me.

There is a problem with this solution. Say, you want to handle subdomains as different users: routes.Add("SD", new DomainRoute("user}.localhost", "", new { controller = "Home", action = "IndexForUser", user="u1" } )); It caches the homepage as well. This is because of the regex that's generated. In order to fix this, you can make a copy of the CreateRegex method in DomainRoute.cs, name it CreateDomainRegex, change the * on this line to +: source = source.Replace("}", @">([a-zA-Z0-9_]*))"); and use this new method for domain regx in GetRouteData method: domainRegex = CreateDomainRegex(Domain);
–
Gorkem PacaciJun 28 '10 at 20:12

I don't know why I can't run this code... I just receive SERVER NOT FOUND error... means the code is not working for me... are you setting any other configuration or something?!
–
Dr TJAug 5 '14 at 12:02

To capture the subdomain when using Web API, override the Action Selector to inject a subdomain query parameter. Then use the subdomain query parameter in your controllers' actions like this:

public string Get(string id, string subdomain)

This approach makes debugging convenient since you can specify the query parameter by hand when using localhost instead of the actual host name (see the standard MVC5 routing answer for details). This is the code for Action Selector:

Typically the route is not aware of the domain because the application could be deployed to any domain and the route would not care one way or another. But in your case you want to base the controller and action off the domain, so you will have to create a custom route that is aware of the domain.