+3  A: 

Yes but you have to create your own route handler.

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.

Nick Berardi
A: 

I searched online and find this question asked in many forums but there is no one actually offer a solution :(

Is this a mission impossible? Can somebody offer a concrete guide on this? Thanks a lot!

http://forums.asp.net/t/1355799.aspx

ycseattle
+37  A: 

You can do it by creating a new route and adding it to the routes collection in RegisterRoutes in your global.asax. Below is a very simple example of a custom Route:

public class ExampleRoute : RouteBase
{

    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
        var url = httpContext.Request.Headers["HOST"];
        var index = url.IndexOf(".");

        if (index < 0)
            return null;

        var subDomain = url.Substring(0, index);

        if (subDomain == "user1")
        {
            var routeData = new RouteData(this, new MvcRouteHandler());
            routeData.Values.Add("controller", "User1"); //Goes to the User1Controller class
            routeData.Values.Add("action", "Index"); //Goes to the Index action on the User1Controller

            return routeData;
        }

        if (subDomain == "user2")
        {
            var routeData = new RouteData(this, new MvcRouteHandler());
            routeData.Values.Add("controller", "User2"); //Goes to the User2Controller class
            routeData.Values.Add("action", "Index"); //Goes to the Index action on the User2Controller

            return routeData;
        }

        return null;
    }

    public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
    {
        //Implement your formating Url formating here
        return null;
    }
}
Jon Cahill
+10  A: 

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 Sampson
+2  A: 

This is not my work, but I had to add it on this answer.

Here is a great solution to this problem. Maartin Balliauw wrote code that creates a DomainRoute class that can be used very similarly to the normal routing.

http://blog.maartenballiauw.be/post/2009/05/20/ASPNET-MVC-Domain-Routing.aspx

Sample use would be like this - just beautiful...

routes.Add("DomainRoute", new DomainRoute( 
    "{customer}.example.com", // Domain with parameters 
    "{action}/{id}",    // URL with parameters 
    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults 
))

;

Jim Blake
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 Pacaci
+1  A: 

If you're looking for a solution suitable to multi-tenant applications you can also check out: http://lukesampson.com/post/303245177/subdomains-for-a-single-application-with-asp-net-mvc

brainnovative