Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use subdomains with localhost on IISExpress?

I'm serving my MVC app locally via localhost:5252 right now.

I'm trying to set up my app to test multi-tenancy by having both localhost and contoso.localhost point to my web app instance.

I set up my hosts file with the corresponding entry:

127.0.0.1       contoso.localhost 

However I'm not sure what else I need to do to make this work. Right now I just get a standard IIS8 page when I navigate, even though I haven't done anything in IIS Manager.

I'm trying to avoid having to use IIS locally, as everything is working with IISExpress. Is there any easy way to achieve this?

like image 980
SB2055 Avatar asked Nov 05 '13 00:11

SB2055


People also ask

Can you have subdomains on localhost?

You need to set your /etc/hosts or C:\Windows\system32\drivers\etc\hosts (as administrator) to reflect the "subdomain". So add 127.0. 0.1 dev. localhost to either file (depending on your platform).

Where is the IISExpress folder?

This file is located in the %userprofile%\Documents\IISExpress\config folder or %userprofile%\My Documents\IISExpress\config folder, depending on your OS. When you run a site from a configuration file, you can specify which site to run.


2 Answers

This can be accomplished by editing the applicationHost.config file

C:\Users\yourProfile\Documents\IISExpress\config\applicationHost.config 

Visual Studio usually handles editing this file for you when you make configuration changes, but you can manually edit. Find the particular site you are working with and the following bindings should work:

<bindings>    <binding protocol="http" bindingInformation="*:5252:localhost" />    <binding protocol="http" bindingInformation="*:5252:contoso.localhost" /> </bindings> 

You can really change the port to anything you want so you could use 80 to save yourself some typing. Continue adding bindings to fill out your multi-tenancy as needed.

If you are looking for more information, I would recommend checking out Scott Hanselman's blog post on IIS Express & SSL.

Update

With VS 2015 & .NET core, the applicationHost.config has moved to a directory based system of storing the configuration instead of a global configuration file. The new location is relative to your project:

.vs\config\applicationHost.config

The same editing principals apply to this file, but when using TFS source control VS does not edit the file when changing between branches as it did with the global file.

Regarding the wildcard comment for domains, wildcarding hostname isn't supported until IIS 10. Therefore, it may be supported in IIS Express 10, but probably not before.

The breakdown of the bindingInformation is:

bindingInformation="<IPAddress>:<Port>:<Hostname>"

like image 172
beavel Avatar answered Sep 27 '22 23:09

beavel


Try checking your project settings for the Project URL.

Right click on your project in the Solution Explorer -> Properties -> Web

Look for Project Url under Servers. Update this value to mirror your subdomain url and you should be good to go.

like image 32
tarnold86 Avatar answered Sep 27 '22 23:09

tarnold86