313

Traditionally I use custom domains with my localhost development server. Something along the lines of:

dev.example.com
dev.api.example.com

This has provided me a ton of flexibility when working with external API's such as Facebook. This has worked great in the past with the built in Visual Studio Development Server, because all I needed to do was add a CNAME to those DNS records pointing to 127.0.0.1.

However I have not been able to get this to work with IIS Express. Everything I have tried seems to have failed. I have even added the correct XML config to the applicationHost.config file for IIS Express, but it doesn't seem to recognize the entries as valid as a true install of IIS would.

<binding protocol="http" bindingInformation="*:1288:dev.example.com" />

Whenever I enter this line and try to request http://dev.example.com:1288 I get the following message:

Bad Request - Invalid Hostname

Does anybody know if I am missing something obvious? Or did the IIS Express team really lack the foresight to see this type of use?

Luis Perez
  • 26,159
  • 10
  • 76
  • 77
Nick Berardi
  • 52,504
  • 14
  • 109
  • 135
  • Make sure you have the applicationPool attribute of the application node set to either "Clr2IntegratedAppPool" or "Clr4IntegratedAppPool". I got the Bad Hostname error you are seeing when using "Clr2ClassicAppPool" or "Clr4ClassicAppPool". – Neil Monroe Feb 18 '11 at 18:46
  • confused - CNAME records do not accept ip addresses but other host names instead. Did you mean A record? – Scott Coates Mar 03 '12 at 01:17
  • 1
    I had lots of issues with this and found it much easier to just use IIS instead of IIS Express – user0b101010 Mar 23 '12 at 14:42
  • I just ran into this as well. I was hoping it would just look at the port number and ignore the domain. No such luck. Wish I could enable "accept all" on the port.... – Brady Moritz May 28 '13 at 01:31
  • Vote now! I've added a UserVoice suggestion: https://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/7763535-easy-iis-express-with-custom-domains – Luke Puplett Apr 30 '15 at 09:38
  • This has been working great for me in VS2013, but I just realized its broken in 2015 (at least for me) - same project, same machine. It looks like IISExpress when launched from VS2015 is looking for applicationHost.config in {Solution Folder}\{Project Folder}\.vs\config. Making changes to that version of the file appears to get thing up and working again. – David W Gray Sep 09 '15 at 02:23

15 Answers15

373

This is what worked for me (Updated for VS 2013, see revision history for 2010, for VS 2015 see this: https://stackoverflow.com/a/32744234/218971):

  1. Right-click your Web Application Project ▶ PropertiesWeb, then configure the Servers section as follows:

    • Select IIS Express ▼ from the drop down
    • Project Url: http://localhost
    • Override application root URL: http://dev.example.com
    • Click Create Virtual Directory (if you get an error here you may need to disable IIS 5/6/7/8, change IIS's Default Site to anything but port :80, make sure Skype isn't using port 80, etc.)
  2. Optionally: Set the Start URL to http://dev.example.com

  3. Open %USERPROFILE%\My Documents\IISExpress\config\applicationhost.config (Windows XP, Vista, and 7) and edit the site definition in the <sites> config block to be along the lines of the following:

    <site name="DevExample" id="997005936">
        <application path="/" applicationPool="Clr2IntegratedAppPool">
            <virtualDirectory
                path="/"
                physicalPath="C:\path\to\application\root" />
        </application>
        <bindings>
            <binding
                protocol="http"
                bindingInformation=":80:dev.example.com" />
        </bindings>
        <applicationDefaults applicationPool="Clr2IntegratedAppPool" />
    </site>
    
  4. If running MVC: make sure the applicationPool is set to one of the "Integrated" options (like "Clr2IntegratedAppPool").

  5. Open your hosts file and add the line 127.0.0.1 dev.example.com.

  6. ► Start your application!

Some great advice from the comments:

  • You may need to run Visual Studio as Administrator.
  • If you want to make other devs see your IIS run netsh http add urlacl url=http://dev.example.com:80/ user=everyone
  • If you want the site to resolve for all hosts set bindingInformation="*:80:". Use any port you want, 80 is just convenient. To resolve all hosts you'll need to run Visual Studio as an administrator
stimms
  • 39,499
  • 27
  • 88
  • 144
David Murdoch
  • 82,194
  • 38
  • 141
  • 186
  • 12
    This is exactly what I did, was about to write this up, but you beat me to the punch :-) As a side note, if you want to let other dev's see your IIS you may want to do something like "netsh http add urlacl url=http://dev.example.com:80/ user=everyone" – EBarr Apr 07 '11 at 16:00
  • @EBarr, do you have any problems after restarting your computer? Visual Studio 2010 seems to forget about this application binding in applicationhost.config and requires that it adds a new one before opening the project. – David Murdoch Apr 07 '11 at 20:24
  • i just restarted and I'm not having any trouble. The bindings remained. This is a longshot, b/c I the setting should be grayed out, but any chance the "auto-assign Port" setting is set under "Use Visual Studio Development Server" ? I just compared my proj file in TFS and remembered I had turned off auto ports prior to this change. – EBarr Apr 07 '11 at 21:03
  • 6
    Also, if you want the site to resolve for all hosts, you can use: bindingInformation="*:80:" I use this so localtunnel.com works. – Lance Fisher Jan 02 '12 at 00:36
  • 17
    I was getting an access denied error. Running VS 2012 as admin fixed it. – xanadont Oct 06 '12 at 21:28
  • 3
    what version of IIS Express is this? I'm running VS2012 (which I believe is IIS Express 8.0), and when I click on the "Use Local IIS Server" radiobutton under the "Servers" area of the Web properties page, click the Use IIS Express checkbox, and then try to put in "http://mydev.example.com", then hit the "Create Virtual Directory" button, I get this error "Unable to create the virtual directory. Cannot create the Web site mydev.example.com". You must specify "localhost" for the server name. – Michael McCarthy May 01 '13 at 15:40
  • This answer applies to Visual Studio 2010, sorry. – David Murdoch May 01 '13 at 17:26
  • 12
    This is complicated. One must change seetings on several different places. It is easy to make a real mess. Is there really no simpler way? – Anderson May 27 '13 at 08:10
  • 3
    Just a note that this does work in 2012. Thanks for the writeup! – Shawn Hubbard Jun 22 '13 at 17:44
  • You do not have to use port 80 to test your sub-domains. I followed the steps in the answer but used port 60486 instead of port 80. The URL is http:// subdomain1.somedomain.com:60486/ – Bassem Jul 10 '13 at 13:07
  • I used port 80 so I could rig my form URLs up to the actual address (without port #) in development. It sounds complicated, but you can take a multi-project solution and probably rig the entire thing in 5 minutes. It worked for me in VS 2012 Express, but I had to restart VS before IIS Express would work properly. – RyanJMcGowan Sep 09 '13 at 08:43
  • 1
    you can following the article http://code.developwithus.com/dotnet/aspnet/debugopen-website-remotely-in-iis-express/ that works – masadi zainul Sep 19 '13 at 12:15
  • After I made these changes my service does not work no more! :( – GVillani82 Mar 20 '14 at 09:06
  • I use Windows 7, VS2012 and the %USERPROFILE%\My Documents\IISExpress\config\applicationhost.config does NOT exist in my machine (NO "IIS Express" in "My Documents"). I found applicationhost.config in c:\Program Files\IIS Express but it didn't contain definitions of my Visual Studio website. I hit on create Virtual Directory. Any ideas? – firepol Apr 23 '14 at 06:29
  • 1
    Works in VS 2013 but: If the IIS is installed the default website must be stopped. Otherwise port 80 is bound to this website. Visual Studio must be run under the admin account to allow the binding, at least unter Windows 8.1. – Jürgen Bayer Aug 23 '14 at 09:25
  • Am I missing something here? VIEW > Properties Window > Web doesn't exist and if I set a domain in the Project Url and click Create Virtual Directory I get the same "must specify localhost" error that @indiecodemonkey mentions above. Is this answer right for VS 2013.4? – Luke Puplett Apr 30 '15 at 09:48
  • @LukePuplett, What is the tab with the "Project URL" property named? Are you using VS 2013 Express, Premium, Pro, or Ultimate? – David Murdoch May 04 '15 at 13:19
  • @DavidMurdoch Hi David, the tab is named Web in the project properties editor (where Project Url input control is), but this isn't arrived at via VIEW > Properties Window. This is 2013.4 Premium. It's the error that's obviously the blocker. – Luke Puplett May 05 '15 at 13:52
  • @DavidMurdoch, hello m using visual studio 2013, but its showing error that > unable to launch iis webserver > the start url specified is not valid – Ziyad Kashif May 11 '15 at 14:32
  • @ZiyadKashif, I'm wondering if the latest VS 2013 update has disabled this workaround. 'm running VS 2013 with Update 3 (Update 4 is latest). Try leaving the Start URL set to `localhost` then manually editing the `bindingInformation` property in the `applicationhost.config` (shown in Step 3, above) to `:80:dev.example.com`. Let me know if this works for you. – David Murdoch May 11 '15 at 14:36
  • @LukePuplett, see my above comment for a possible workaround. Let me know if it works or not. – David Murdoch May 11 '15 at 14:37
  • @David-Murdoch thanks, I'll try and get to it, working in another area at the mo. – Luke Puplett May 13 '15 at 12:16
  • FYI: This appears to still work in Visual Studio 2015 (at least as of Beta 2). Also, after the changes have been made to applicationhost.config, you can then set the Project URL to `http://dev.example.com/` so that opening the current page will always open the page from the correct domain. – Jeremy Caney May 14 '15 at 23:25
  • 1
    Too complicated, just `applicationhost.config` binding to "*:80:" and be done with it! – Hugh Jeffner May 28 '15 at 19:06
  • 2
    My vote up was entirely for "Run VS as Administrator"... Would could I forget that? All the rest was already set up. Thanks! – nrod Aug 12 '15 at 14:15
  • Can you update your post to be compatible with VS 2015? I'm trying to set up a multitenant app, and I want to able to support direction of ANY subdomain to my website. – Shimmy Weitzhandler Aug 13 '15 at 18:26
  • 2
    For Visual Studio 2015 the steps are exactly the same except that the applicationhost.config file is located under the project folder /.vs/config/applicationhost.config – Bassem Aug 23 '15 at 18:49
  • 1
    Why is "Project Url" set to "localhost"? That means that when you run from Visual Studio it opens up "http://localhost" instead of "dev.example.com". I've set "Project Url" to "dev.example.com" and un-checked the override. – Ian Warburton Oct 08 '15 at 15:28
  • @ianwarburton, it's just what worked for me at the time, your solution send much better. Feel free to add it to the answer! – David Murdoch Oct 08 '15 at 16:24
  • Also I found that I needed "netsh http add urlacl" even on my own machine. – Ian Warburton Oct 08 '15 at 17:07
  • 3
    Just a note: in VS 2015, the applicationhost.config file is located under //yourProject/.vs/config – danpop Feb 18 '16 at 11:15
  • Under windows 10 with VS 2017, the application.config file is located in $(solutionDir)\.vs\config\applicationhost.config see this article for further detail; https://stackoverflow.com/questions/12946476/where-is-the-iis-express-configuration-metabase-file-found – Shawson Jul 10 '17 at 13:45
161

For Visual Studio 2015 the steps in the above answers apply but the applicationhost.config file is in a new location. In your "solution" folder follow the path, this is confusing if you upgraded and would have TWO versions of applicationhost.config on your machine.

\.vs\config

Within that folder you will see your applicationhost.config file

Alternatively you could just search your solution folder for the .config file and find it that way.

I personally used the following configuration:

enter image description here

With the following in my hosts file:

127.0.0.1       jam.net
127.0.0.1       www.jam.net

And the following in my applicationhost.config file:

<site name="JBN.Site" id="2">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="C:\Dev\Jam\shoppingcart\src\Web\JBN.Site" />
    </application>
    <bindings>
        <binding protocol="http" bindingInformation="*:49707:" />
            <binding protocol="http" bindingInformation="*:49707:localhost" /> 
    </bindings>
</site>

Remember to run your instance of visual studio 2015 as an administrator! If you don't want to do this every time I recomend this:

How to Run Visual Studio as Administrator by default

I hope this helps somebody, I had issues when trying to upgrade to visual studio 2015 and realized that none of my configurations were being carried over.

Antoine Meltzheim
  • 8,525
  • 6
  • 31
  • 39
njfife
  • 3,392
  • 1
  • 16
  • 28
  • 10
    The crucial part is to *add* the second `*:49707:` binding instead of *replacing* the default `*:49707:localhost` as suggested in many other answers. And you have to run VS as an administrator, indeed (sic!). Thank you. – fracz Jun 10 '16 at 08:32
  • I'm running VS 2015 and dont have the IISExpress drop down. I think that is because I have a 'web site project' as opposed to a 'web app project'. – Tom McDonald Jul 04 '16 at 15:15
  • 3
    You could also use a URL like `http://jam.127.0.0.1.xip.io/` if you don't want modify your hosts file. Plus, any collaborators won't need to modify theirs. See http://xip.io/. – Zack Martin Aug 11 '16 at 16:12
  • I've been fighting with this for as long as I can remember and still cannot get it to work. I've followed the directions exactly but just end up with the error "This site can’t be reached. mysite.fr.local refused to connect". In previous versions of Visual Studio I had this working fine. – ProNotion Jan 31 '17 at 12:36
  • 1
    I think it worth to note that folder ".vs" is hidden and should be made visible at first – Disappointed Mar 15 '17 at 10:53
  • 3
    I have confirmed that this works in Visual Studio 2017 as well. Interestingly, I did not need both elements (just the first one without "localhost"). Visual Studio, however, certainly does need to be running as Administrator, which is sort of annoying – Loren Paulsen Apr 30 '17 at 23:13
  • 1
    I just wish there was a way for those of us who are not allowed to run anything administratively to do this. Employers/clients who blanket ban admin privilege without any thought as to how they might safely enable this are incredibly short sighted. – Steve Pettifer Jul 14 '17 at 08:41
  • 1
    This solved it for me in VS 2017 as well - I had to add ``, ie WITHOUT localhost – Muleskinner Sep 25 '17 at 13:03
  • I've been fighting with this for months on and off and just can't get it to work since upgrading to VS2017. If I simply add the binding `` and then try to run the project it says unable to launch port 80 is in use and adds a new site and binding `` in the applicationHost.config for the solution. – ProNotion Jun 06 '18 at 08:39
  • If you run `netsh http add urlacl url=http://www.jam.net:49707/ user=everyone ` as admin, you then do not have to run VS as admin each time. You can also revert to just using port 80 as well since you are use host header. – John C Jul 25 '20 at 03:50
44

When using Visual Studio 2012 with IIS Express, changing an existing binding does not work permanently. (It may work until you close VS, but after that, things get really messed up.)

The key is keeping the existing localhost binding and adding a new binding after it.

Unless you're running as administrator, you'll also need to run netsh add urlacl (to give yourself permissions to run a non-localhost site as a standard user).

If you want to allow any host name, the full process is as follows:

  1. Create your web application, and find out what port it is using (see project properties, Web tab, Project Url).
  2. From an administrator prompt, run the following commands (replacing portnumber with the port number you figured out in #1):

    netsh http add urlacl url="http://*:portnumber/" user=everyone
    netsh http add urlacl url="http://localhost:portnumber/" user=everyone
    

You can also use your user name (DOMAIN\USER) instead of everyone for better security.

  1. Open applicationhost.config (usually under My Documents\IIS Express\config), and find the element with your port number.
  2. Add one more binding with the host name you want (in this case, *). For example:

    <site name="MvcApplication1" id="2">
        <application path="/" applicationPool="Clr4IntegratedAppPool">
            <virtualDirectory path="/" physicalPath="C:\sites\MvcApplication1" />
        </application>
        <bindings>
            <binding protocol="http" bindingInformation="*:12853:localhost" />
            <binding protocol="http" bindingInformation="*:12853:*" />
        </bindings>
    </site>
    

Note that, if want to open up all host names (*), you'll need two netsh commands (one for * and one for localhost). If you only want to open up a specific host name, you don't strictly need the second netsh command (localhost); just the one with your specific host name is sufficient.

Jon
  • 7,185
  • 6
  • 45
  • 66
dmatson
  • 5,861
  • 1
  • 21
  • 22
  • Did the same (1st approach), when browsing to http://mycustomhost:myportnr I get "Service Unavailable". VS2012. I can't try the 2nd as there is no IIS Express under "My Documents" and applicationhost.config I found in c:\Program files\IIS Express had no config for my web application... – firepol Apr 23 '14 at 06:44
  • 2
    With VS2013, I find that this configuration only works if I then run as admin! (Running as non-admin, IIS Express seems to ignore the 2nd binding - if I open its UI from the notification icon, IIS shows my site as running, but only shows the first binding, and I can only use http://localhost... If I'm running as admin, it shows both bindings in the UI, and both work.) I've added the urlacls with netsh, but this appears to make no difference - IIS Express seems not to even try and bind anything other than localhost if it's not elevated. – Ian Griffiths Sep 29 '14 at 06:41
  • Adding a record with the `*` solved the "Service Unavailable" it for me. – jerone Nov 02 '17 at 11:26
  • What about a scenario that you need to use HTTPS on port 443? – Kixoka May 09 '18 at 16:16
  • @Kixoka I am trying to run HTTPS. I managed to get it working but the browser complains that the certificate does not match the domain. I get past that but all resources then fail to load so no images, scripts or CSS. – K-Dawg Aug 12 '18 at 08:32
24

The invalid hostname indicates that the actual site you configured in the IIS Express configuration file is (most likely) not running. IIS Express doesn't have a process model like IIS does.


For your site to run it would need to be started explicitly (either by opening and accessing from webmatrix, or from command line calling iisexpress.exe (from it's installation directory) with the /site parameter.


In general, the steps to allow fully qualified DNS names to be used for local access are Let's use your example of the DNS name dev.example.com

  1. edit %windows%\system32\drivers\etc\hosts file to map dev.example.com to 127.0.0.1 (admin privilege required). If you control DNS server (like in Nick's case) then the DNS entry is sufficient as this step is not needed.
  2. If you access internet through proxy, make sure the dev.example.com will not be forwared to proxy (you have to put in on the exception list in your browser (for IE it would be Tools/Internet Options/Connections/Lan Settings, then go to Proxy Server/Advanced and put dev.example.com on the exeption list.
  3. Configure IIS Express binding for your site (eg:Site1) to include dev.example.com. Administrative privilege will be needed to use the binding. Alternatively, a one-time URL reservation can be made with http.sys using

    netsh http add urlacl url=http://dev.example.com:<port>/ user=<user_name>

  4. start iisexpress /site:Site1 or open Site1 in WebMatrix

Jaro Dunajsky
  • 1,995
  • 13
  • 8
  • 2
    Hi Jaro, thanks for the help, but 127.0.0.1 is set in my DNS not the hosts file so that all the developers have the same access. Second I cannot get it to start debugging in VS, unless the binding is set as "*:1234:localhost" which is a totally useless setup, because it is bound to all IP addresses on my machine but only accepts host headers of "localhost". Why didn't they just do "127.0.0.1:1234:" and avoid all this crap. Host headers are not needed as long as the IP and port resolve. And running as Admin is just a huge pain in the ass for a large team. What a disaster IIS Express is. – Nick Berardi Jan 18 '11 at 00:56
  • 1
    Also just as a reference `Bad Request - Invalid Hostname` means that the hostname I am trying to use cannot resolve because the IIS Express team in their infinite wisdom decided to specifically call out "localhost" as the only host header that would work. And VS won't let you run it any other way other than "*:1234:localhost" – Nick Berardi Jan 18 '11 at 00:58
  • 2
    I hear you on the 127.0.0.1 issue. The "localhost" in the binding is definitely a pain since it doesn't include traffic for http://127.0.0.1 . The design is limited by today's contract of http.sys (http protocol handler) that only recognizes "localhost" for the non-administrative use. The admin only requirement can be mitigated by one-time URL reservation with http.sys (will update the information above). Please let me know you exact use with Visual Studio (it is VS2010 with SP1 Beta?). Let's try to work on this some more and (hopefully) settle the issue. – Jaro Dunajsky Jan 18 '11 at 02:49
  • note - if you see netsh.exe error 87 the parameter is incorrect - may be because you omitted an URI path (/) after your hostname. – Tim Lovell-Smith Nov 28 '12 at 22:58
19

On my WebMatrix IIS Express install changing from "*:80:localhost" to "*:80:custom.hostname" didn't work ("Bad Hostname", even with proper etc\hosts mappings), but "*:80:" did work--and with none of the additional steps required by the other answers here. Note that "*:80:*" won't do it; leave off the second asterisk.

rymo
  • 3,039
  • 2
  • 30
  • 38
  • 3
    uuuum, so `"*:80:*"` worked for me, but then I restarted my computer and it suddenly wouldn't work until I changed it to `"*:80:"` and all was well! Thanks. – CWSpear Jul 13 '13 at 04:59
  • 2
    Finally!! Also followed all the steps elsewhere, and removing the * at the end made it work, thanks! – Charles Philip Jun 13 '14 at 13:04
  • Just here to say that this worked for me. I tried the accepted answer and reverted all changes down to what you said and all works great. I am using VS2013 Update4. – JasonWilczak Sep 29 '15 at 14:51
  • This seems to work perfectly. When using VS2017, I'm not sure what to place in the Project Url to have it write that in the applicationhost.config file? Is it possible to set through VS IDE? – Jim Jan 23 '18 at 17:12
9

Like Jessa Flint above, I didn't want to manually edit .vs\config\applicationhost.config because I wanted the changes to persist in source control. I also didn't want to have a separate batch file. I'm using VS 2015.

Project Properties→Build Events→Pre-build event command line: Screenshot of project properties


::The following configures IIS Express to bind to any address at the specified port

::remove binding if it already exists
"%programfiles%\IIS Express\appcmd.exe" set site "MySolution.Web" /-bindings.[protocol='http',bindingInformation='*:1167:'] /apphostconfig:"$(SolutionDir).vs\config\applicationhost.config"

::add the binding
"%programfiles%\IIS Express\appcmd.exe" set site "MySolution.Web" /+bindings.[protocol='http',bindingInformation='*:1167:'] /apphostconfig:"$(SolutionDir).vs\config\applicationhost.config"

Just make sure you change the port number to your desired port.

Jon
  • 7,185
  • 6
  • 45
  • 66
  • One minor addition here. Make sure to create rules to open that port in your Windows Firewall. Then it worked like a charm! – Peter M Aug 31 '16 at 20:38
  • 1
    I like your answer because editing the .vs\config\applicationhost.config is not feasible when multiple developers run IIS Express on their own machines. Thank you! – Colin Mar 07 '17 at 02:02
8

I was trying to integrate the public IP Address into my workflow and these answers didn't help (I like to use the IDE as the IDE). But the above lead me to the solution (after about 2 hours of beating my head against a wall to get this to integrate with Visual Studio 2012 / Windows 8) here's what ended up working for me.

applicationhost.config generated by VisualStudio under C:\Users\usr\Documents\IISExpress\config

    <site name="MySite" id="1">
        <application path="/" applicationPool="Clr4IntegratedAppPool">
            <virtualDirectory path="/" physicalPath="C:\Users\usr\Documents\Visual Studio 2012\Projects\MySite" />
        </application>
        <bindings>
            <binding protocol="http" bindingInformation="*:8081:localhost" />
            <binding protocol="http" bindingInformation="*:8082:localhost" />
            <binding protocol="http" bindingInformation="*:8083:192.168.2.102" />
        </bindings>
    </site>
  • Set IISExpress to run as Administrator so that it can bind to outside addresses (not local host)
  • Run Visual Stuio as an Administrator so that it can start the process as an administrator allowing the binding to take place.

The net result is you can browse to 192.168.2.102 in my case and test (for instance in an Android emulator. I really hope this helps someone else as this was definitely an irritating process for me.

Apparently it is a security feature which I'd love to see disabled.

Daniel B. Chapman
  • 4,401
  • 29
  • 38
6

The up-voted answer is valid.. and this information helped me quite a bit. I know this topic has been discussed before but I wanted to add some additional input. People are saying that you must "manually edit" the application.config file in the Users IISExpress/Config directory. This was a big issue for me because I wanted to distribute the configuration via Source control to various developers.

What I found is that you can automate the updating of this file using the "C:\Program Files\IIS Express\appcmd.exe" program. It took a while to find out the control parameters but Ill share my findings here. Essentially you can make a .bat file that runs both the NETSH command and the APPCMD.EXE (and perhaps swap out a host file if you like) to make host header configuration easy with IIS Express.

Your install bat file would look something like this:

netsh http add urlacl url=http://yourcustomdomain.com:80/ user=everyone 

"C:\Program Files\IIS Express\appcmd.exe" set site "MyApp.Web" /+bindings.[protocol='http',bindingInformation='*:80:yourcustomdomain.com']

I also will make a "Uninstall" bat file that will clean up these bindings..(because often times Im just faking out DNS so that I can work on code that is host name sensitive)

netsh http delete urlacl url=http://yourcustomdomain.com:80/

"C:\Program Files\IIS Express\appcmd.exe" set site "MyApp.Web" /-bindings.[protocol='http',bindingInformation='*:80:yourcustomdomain.com']

I hope this information is helpful to someone.. It took me a bit to uncover.

Jesse Flint
  • 61
  • 1
  • 2
4

Following Jaro's advice, I was able to get this working under Windows XP and IIS Express (installed via Web Matrix) with a small modification and was not limited to only localhost. It's just a matter of setting the bindings correctly.

  1. Use WebMatrix to create a new site from folder in your web application root.
  2. Close WebMatrix.
  3. Open %USERPROFILE%\My Documents\IISExpress\config\applicationhost.config (Windows XP. Vista and 7 paths will be similar) and edit the site definition in the <sites> config block to be along the lines of the following:

    <site name="DevExample" id="997005936">
        <application path="/" applicationPool="Clr2IntegratedAppPool">
            <virtualDirectory
                path="/"
                physicalPath="C:\path\to\application\root" />
        </application>
        <bindings>
            <binding
                protocol="http"
                bindingInformation="*:80:dev.example.com" />
        </bindings>
        <applicationDefaults applicationPool="Clr2IntegratedAppPool" />
    </site>

If running MVC, then keep the applicationPool set to one of the "Integrated" options.

Neil Monroe
  • 1,206
  • 1
  • 14
  • 20
3

David's solution is good. But I found the <script>alert(document.domain);</script> in the page still alerts "localhost" because the Project Url is still localhost even if it has been override with http://dev.example.com. Another issue I run into is that it alerts me the port 80 has already been in use even if I have disabled the Skype using the 80 port number as recommended by David Murdoch. So I have figured out another solution that is much easier:

  1. Run Notepad as administrator, and open the C:\Windows\System32\drivers\etc\hosts, add 127.0.0.1 mydomain, and save the file;
  2. Open the web project with Visual Studio 2013 (Note: must also run as administrator), right-click the project -> Properties -> Web, (lets suppose the Project Url under the "IIS Express" option is http://localhost:33333/), then change it from http://localhost:33333/ to http://mydomain:333333/ Note: After this change, you should neither click the "Create Virtual Directory" button on the right of the Project Url box nor click the Save button of the Visual Studio as they won't be succeeded. You can save your settings after next step 3.
  3. Open %USERPROFILE%\My Documents\IISExpress\config\applicationhost.config, search for "33333:localhost", then update it to "33333:mydomain" and save the file.
  4. Save your setting as mentioned in step 2.
  5. Right click a web page in your visual studio, and click "View in Browser". Now the page will be opened under http://mydomain:333333/, and <script>alert(document.domain);</script> in the page will alert "mydomain".

Note: The port number listed above is assumed to be 33333. You need to change it to the port number set by your visual studio.

Post edited: Today I tried with another domain name and got the following error: Unable to launch the IIS Express Web server. Failed to register URL... Access is denied. (0x80070005). I exit the IIS Express by right clicking the IIS Express icon at the right corner in the Windows task bar, and then re-start my visual studio as administrator, and the issue is gone.

TylerH
  • 19,065
  • 49
  • 65
  • 86
wcb1
  • 537
  • 1
  • 6
  • 6
  • 1
    now, *this* is the answer that should be considered as the proper solution ! Make sure you edit the applicationhost.config located in .vs/config if you are using visual studio 2015. – Fabio Salvalai Sep 02 '16 at 14:48
2

This method has been tested and worked with ASP.NET Core 3.1 and Visual Studio 2019.

.vs\PROJECTNAME\config\applicationhost.config

Change "*:44320:localhost" to "*:44320:*".

<bindings>
    <binding protocol="http" bindingInformation="*:5737:localhost" />
    <binding protocol="https" bindingInformation="*:44320:*" />
</bindings>

Both links work:

Now if you want the app to work with the custom domain, just add the following line to the host file:

C:\Windows\System32\drivers\etc\hosts

127.0.0.1 customdomain

Now:

  • https://customdomain:44320

NOTE: If your app works without SSL, change the protocol="http" part.

Haddad
  • 55
  • 3
  • 1
    I get "Unable to connect to web server 'IIS Express'." because of that asterisk – jokab Sep 24 '20 at 11:17
  • you unfortunately *must* run visual studio as administrator for this to work. I have tried the netssh thing and other stuff and only thing that has worked is running visual studio as administrator – Jacqueline Loriault Oct 20 '20 at 15:28
1

I tried all of above, nothing worked. What resolved the issue was adding IPv6 bindings in the hosts file. In step 5 of @David Murdochs answer, add two lines instead of one, i.e.:

127.0.0.1 dev.example.com
::1 dev.example.com

I figured it out by checking $ ping localhost from command line, which used to return:

Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Instead, it now returns:

Reply from ::1: time<1ms

I don't know why, but for some reason IIS Express started using IPv6 instead of IPv4.

1

Just in case if someone may need...

My requirement was:

  • SSL enabled
  • Custom domain
  • Running in (default) port: 443

Setup this URL in IISExpress: http://my.customdomain.com

To setup this I used following settings:

Project Url: http://localhost:57400

Start URL: http://my.customdomain.com

/.vs/{solution-name}/config/applicationhost.config settings:

<site ...>
    <application>
        ...
    </application>
    <bindings>
        <binding protocol="http" bindingInformation="*:57400:" />
        <binding protocol="https" bindingInformation="*:443:my.customdomain.com" />
    </bindings>
</site>
dan
  • 1,203
  • 3
  • 17
  • 21
0

I was using iisexpress-proxy (from npm) for this.

https://github.com/icflorescu/iisexpress-proxy

trd
  • 87
  • 7
0

Leaving this here just in case anyone needs...

I needed to have custom domains for a Wordpress Multisite setup in IIS Express but nothing worked until I ran Webmatrix/Visual Studio as an Administrator. Then I was able to bind subdomains to the same application.

<bindings> 
    <binding protocol="http" bindingInformation="*:12345:localhost" />
    <binding protocol="http" bindingInformation="*:12345:whatever.localhost" />
</bindings>

Then going to http://whatever.localhost:12345/ will run.

MW247
  • 1
  • 2