6

I want to test some subdomain features in an asp.net site. I read just to edit the hosts file and add 127.0.0.1 subdomain.domain.com and that should work.

But mine seems to not. I have flushed the dns, restarted, tried new browsers and still get error 500.

When I normal run the asp.net site from Visual Studio it goes to http://localhost:17365/ . How do I get the IP address for this?

I guess I don't get the whole picture and missing some pieces.

I ran netstat -n and see that the site is running on[::1]:17365 maybe thats why its not working under ipv4?

Zach
  • 348
  • 3
  • 7
  • 22
  • have you looked at `Dns.GetHostAddresses` – MethodMan Apr 05 '13 at 14:46
  • you could try something like this but personally you need a real address try something like `www.google.com` in this example `foreach (IPAddress address in Dns.GetHostAddresses("www.google.com")) { Console.WriteLine(address.ToString()); }` – MethodMan Apr 05 '13 at 14:58

10 Answers10

6

The solution is very simple. After you have edit your host files adding a line as:

127.0.0.1 subdomain.domain.com

you run your site, the personal web starts and you get on your browser this

http://localhost:[PortNumber]/ (eg: http://localhost:17365/ )

just change it to

http://subdomain.domain.com:[PortNumber]/ (eg:http://subdomain.domain.com:17365/)

with your hand, type it on the url on your browser, and it will work.

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • 3
    I get Bad Request - Invalid Hostname HTTP Error 400. The request hostname is invalid. I put in 127.0.0.1 test.domain.com then went to http://test.domain.com:17365/ – Zach Apr 05 '13 at 15:12
  • @Zach Is working to me ! I tested before, and now. Please note, every time you start the debug, and the personal web server the number is change ! Place the new number. – Aristos Apr 05 '13 at 15:24
  • 1
    Yeah Something must be screwed up with my setup. I think the problem is with 127.0.0.1. I try to go to 127.0.0.1:17365 and get a "Bad Request - Invalid Hostname" – Zach Apr 05 '13 at 15:45
5

In visual studio when you go to run the site first change in the Website Properties > Use Dynamic PortstoFalse, then set the Port Numberto80, then run it.

It will by default launch http://localhost:80, however then in your browser visit subdomain.domain.com in your browser it will point to your debugging instance of your site (assuming you've still got that 127.0.0.1 subdomain.domain.com in your hosts file).

Ryan McDonough
  • 9,732
  • 3
  • 55
  • 76
2

I just go to Project Properties > Web and under Servers, I select Use Local IIS Web server and set the Project Url to subdomain.domain.com.

enter image description here

Code Maverick
  • 20,171
  • 12
  • 62
  • 114
  • That could work, would just add more steps when i'm testing multiple subdomains. – Zach Apr 05 '13 at 14:56
  • Depends on what you mean by testing. If you mean debugging, then yes. But, if you just mean visually and functionally, you don't need to even use Visual Studio at all to view your site. Since you changed your hosts file, you can open up any browser and navigate to `subdomain.domain.com` and it will pull up your site as long as you have it set up in IIS. – Code Maverick Apr 05 '13 at 15:02
  • I tried that but is says 'Unable to create the virtual directory. Cannot create the web site 'http://sub.domain.com'. You must specift "localhost" for the server name. – Zach Apr 05 '13 at 17:52
  • That's because you need to go into IIS and add the website there first. Have you not done that before? What version of IIS do you run? – Code Maverick Apr 05 '13 at 18:16
  • I never installed an IIS so it's just using IIS Express. I just installed IIS 8 and will see about adding the site to it. – Zach Apr 05 '13 at 18:23
2

Someone has registered "lvh.me" that point to 127.0.0.1

So you can use: http://lvh.me http://something.lvh.me

joel1di1
  • 773
  • 5
  • 13
0

You have to configure your web server also. Ok, you changed the hosts file. Your system (dns) knows where to go (127.0.0.1) with the host. But your web server still know nothing about subdomain.domain.com and associates nothing with it.

Andrey Volk
  • 3,513
  • 2
  • 17
  • 29
0

When you normally start an asp.net site in Visual Studio it uses the Cassini webserver. I'm not sure if you can configure bindings for Cassini.

My suggestion would be to configure a website or virtual directory in IIS with bindings to subdomain.domain.com, look here on how to do that: Add a binding to a site

Your hosts file should contain this:

127.0.0.1    subdomain.domain.com

The request of subdomain.domain.com will then be redirected to your Local IIS Web server (on port 80), because of the binding configuration IIS knows where to look for your website.

Within the properties of your web project in Visual Studio you can configure to use Local IIS Web server in the Web tab.

Andrew
  • 5,395
  • 1
  • 27
  • 47
  • In the Web config it is set to Use Local ISS Web Server. Use ISS Express is checked with project URL: http://localhost:17365/. – Zach Apr 05 '13 at 17:51
0

I'm not sure that the Visual Studio Development Server can handle bindings to anything other than localhost. I think the only thing you can change is the port number.

You have a couple of options.

1) Install IIS Express (or full-on IIS if you want) and configure Visual Studio to point to the site you have set up in there. You can manually set up your binding to subdomain.domain.com and it's very flexible. Take a look at this answer: Using Custom Domains With IIS Express

2) Install a port forwarder like rinetd (I think this should work if you have the entry in your hosts file). You should be able to map subdomain.domain.com:<some port number> to localhost:17365. Note that rinetd won't detect the domain you're using (just the underlying IP address which is 127.0.0.1), so it's best to use a distinct port for each project you're working on.

Community
  • 1
  • 1
theyetiman
  • 8,514
  • 2
  • 32
  • 41
0

Goto applicationhost.config, you can find on windows tray. Finding application.config of a current running project in IIS

Now find

<webLimits>
......
<sites>
.....
<site name="[YourProjectName]" id="[YourProjectID]">
                <application path="/" applicationPool="Clr4IntegratedAppPool">
                    <virtualDirectory path="/" physicalPath="[Your Project Path]" />
                </application>
                <bindings>
                    <binding protocol="http" bindingInformation="*:50569:[Remove localhost from here]" />
                </bindings>
            </site>
.....
</sites>
......
<webLimits />

Remove localhost from bindingInformation.

Finally, Run VisualStudio as Administrator

Parker
  • 7,244
  • 12
  • 70
  • 92
sisindrymedagam
  • 179
  • 2
  • 8
0

I'm using VS 2017 and Webforms (don't say it).

  1. In the Solution (.sln) file (in notepad) I changed the port to 8080. This was found in several properties, including VWDPort, Debug.AspNetCompiler.VirtualPath, Debug.AspNetCompiler.TargetPath, Release.AspNetCompiler.VirtualPath, and Release.AspNetCompiler.TargetPath
  2. In the applicationhost.config file, I added:
<binding protocol="http" bindingInformation="*:8080:subdomain.domain" />

Also done in notepad. Make sure there are no duplicate or old entries anywhere in this file for bindings. After you change the port in Step 1 you may get an auto-generated duplicate entry with old bindings. Also, be sure you editing the correct applicationhost.config file. This file is in a different place depending on your version of VS.

  1. In Powershell, run: "netsh http add urlacl url=http://subdomain.domain:80/ user=everyone"
  2. In your hosts file (mine is in C:\Windows\System32\drivers\etc) add "127.0.0.1 subdomain.domain" to your hosts listing. You'll have to be an administrator (right-click on notepad, run as administrator)
  3. Run VS as Administrator. This will keep the port to 80. If not run as administrator, you'll get a random port number, your .sln file will get auto-edited with this number, and you'll get duplicate bindings in applicationhost.config.

Running as Administrator is irritating, but it's the only way I could get the port to not change. Also, I tried using other ports, such as the one randomly assigned VS, but kept running into 503 errors from IIS.

George Beier
  • 236
  • 3
  • 8
-1

Using this you could get a list of IP addresses if I add this code it resolves to 127.0.0.1 please confirm if this is what you are looking for otherwise I will remove the answer

foreach (IPAddress address in Dns.GetHostAddresses("localhost"))
{
    Console.WriteLine(address.ToString());
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • I only get one returned value. ::1 – Zach Apr 05 '13 at 19:28
  • I try it on mine and I get local host value 127.0.0.1 you will need to have actual addresses that you can resolve also think about looking into the `HostFile` – MethodMan Apr 05 '13 at 19:32