views:

967

answers:

3

I am trying to develop an asp.net site with multiple subdomains. I am new to web sites in general, and asp.net in particular. However, it seems that wildcard subdomains are properly setup with a combination of dns entries and web server configuration - which isn't going to happen on my development machine. Therefore I'm manually inserting entries in my windows hosts file:

127.0.0.1  localhost
127.0.0.1  abc.localhost
127.0.0.1  xyz.localhost

However, when I try to interrogate the Request.Url property there is no subdomain to be seen. For example, if I hit http://abc.localhost:1660/ in the browser I get http://localhost:1660/ from Request.Uri.ToString(); the abc is just gone?!

I don't know why the hosts file works like this, but is there any other method I can use to get subdomains into my local web application? Thank you all.

Note that I'm only using the built-in asp.net development server rather than a full iis server. (I can't get access to a full IIS this weekend, but I would still like to know if that would help.)

+2  A: 

Don't know about the headers, but one little-known trick that I've used is that all of the 127.* addresses are localhost addresses, not just 127.0.0.1. You can actually run one server listening on 127.0.0.1 port 80, and another webserver instance listening on 127.0.0.2 port 80. So then you'd name 127.0.0.1 localhost, 127.0.0.2 could be abc.localhost (or abc.mydomain.com so you can test the real live URLs against your local webserver), etc.

Joe White
+2  A: 

You can get the requested domain with Subdomain intact by using "Request.Headers["HOST"]". Here's a simple method that returns the Subdomain of the current request. This method also assumes that you have a ".COM", ".NET", etc. after the domain just like the real web. So you'll want to change your HOSTS file to include "localhost.com", "abc.localhost.com", etc.

public string subdomain()
{
    string host = Request.Headers["HOST"];
    if (!string.IsNullOrEmpty(host))
    {
        var parts = host.Split('.');
        if (parts.Length > 2)
        {
            return parts[0];
        }
    }
    return string.Empty;
}

I was searching on this very thing and here's an article that is what actually helped me figure this out: http://blogs.securancy.com/post/ASPNET-MVC-Subdomain-Routing.aspx

Chris Pietschmann
+1  A: 

I am using Windows 7, IIS 7.5, VS 2008, SQL server 2005

I was able to successfully simulate sub-domain on localhost buy putting the following line in etc/hosts

127.0.0.2   myapp.localhost.com

and, in IIS I set up a new web site with the following modifications to the bindings section:

Hostname = myapp.localhost.com 
IP address = 127.0.0.2

I also created a new application pool but it is not necessary. You may have to change the auth user that is used to work on the requests. I had SQL connectivity permission errors with the default IIS user.

IIS>Application Pools>>Right Click on your pool>Advanced Settings>Identity = LocalSystem
Vaibhav Garg