How to use subdomains with localhost on IISExpress

2019-02-05 15:13发布

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?

2条回答
疯言疯语
2楼-- · 2019-02-05 15:53

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>"

查看更多
祖国的老花朵
3楼-- · 2019-02-05 16:08

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.

查看更多
登录 后发表回答