89

I have a test ASP.NET MVC3 application developed in VS2012. When I start debugging the app is accessed from the host machine via the request to http://localhost:<portnumber>. But if I try to access the same application from the remote machine in the intranet via the http://<ip>:<portnumber> I get HTTP error 400: Bad request. Invalid Host Name. As far as it runs on IIS Express any server configuration is inaccessible.

Are there any ways of solving this?

gator88
  • 1,203
  • 2
  • 11
  • 14
  • Use our free extension, Conveyor to do this https://marketplace.visualstudio.com/items?itemName=vs-publisher-1448185.ConveyorbyKeyoti it works without any config changes, painless. – Jim W Mar 08 '18 at 05:22

10 Answers10

143

Update

I made a video that better describes the process, https://youtu.be/5ZqDuvTqQVs

If you are using Visual Studio 2013 or above, make sure you run it as an administrator for this to work.


Open the %USERPROFILE%\My Documents\IISExpress\config\applicationhost.config (in VS2015 it may be $(solutionDir)\.vs\config\applicationhost.config) file. Inside you should see something like this:
<site name="WebSite1" id="1" serverAutoStart="true">
    <application path="/">
        <virtualDirectory path="/" physicalPath="%IIS_SITES_HOME%\WebSite1" />
    </application>
    <bindings>
        <binding protocol="http" bindingInformation="*:8080:localhost" />
    </bindings>
</site>

Change the bindingInformation=":8080:localhost" to bindingInformation="*:8080:*" (the port number, 8080 in my case, will differ.)

Note: If it does not work try with bindingInformation="*:8080: the asterix can be removed.

Then make sure your firewall is allowing incoming connections on that port. You may need to restart the system or at least Visual Studio to get IISExpress to reload the config file.

If this doesn't work, take a look at this answer: https://stackoverflow.com/a/5186680/985284

Community
  • 1
  • 1
Garrett Fogerlie
  • 4,450
  • 3
  • 37
  • 56
  • Good call! This worked perfectly! I had solution working in VS2012, just installed VS2013 and was getting the above error. This helped resolve that issue for me. Thanks! – Tony Aug 26 '13 at 18:06
  • 31
    This solution did not work for me. In VS2013, after modifying the bindingInformation attribute, when I open the IDE and load the project it creates a new entry (i.e. `` and I am still unable to access the website from another compuer. – Yván Ecarri Dec 28 '13 at 11:16
  • 5
    This worked for me in VS2013: http://gilesey.wordpress.com/2013/04/21/allowing-remote-access-to-your-iis-express-service – Jay Cummins Jan 17 '14 at 13:53
  • 11
    Worked for me.The main tweak was to run visual studio as admin. – Tanmay Mandal Mar 07 '14 at 10:57
  • 2
    This also worked for me with VS2013, just make sure you are running VS under the admin account. – Andy Sinclair Mar 31 '14 at 13:38
  • 1
    @Y.Ecarri, I don't suppose you ever figured out how to fix that? – Samuel Edwin Ward Aug 07 '14 at 20:48
  • @SamuelEdwinWard Running VS as an admin fixes that issue. – Garrett Fogerlie Aug 08 '14 at 08:25
  • 21
    I don't know why I seem to have been the only one with this issue, but `bindingInformation="*:8080:*"` did not work for me and resulted in the problem @Y.Ecarri was experiencing. What finally ended up working was dropping the asterisks: `bindingInformation=":8080:"`. Boy did this drive me crazy, I hope it helps someone. – Drazen Bjelovuk Nov 03 '14 at 05:59
  • @JoshBjelovuk Interesting, what version of Visual Studio? – Garrett Fogerlie Nov 04 '14 at 02:23
  • 3
    I found that to prevent VS from creating a new entry I could just duplicate the binding element and have one with `*` and one with `localhost` – byteit101 Jun 17 '15 at 15:20
  • 1
    @byteit101 Interesting, what version of VS are you using? And you don't have to run it as Administrator correct? – Garrett Fogerlie Jun 19 '15 at 01:12
  • @GarrettFogerlie I do have to run it as admin if I want debugging, but 2013 Pro. I can also just launch the iisexpress process as admin if I don't need debugging and leave VS non-admin – byteit101 Jun 19 '15 at 19:00
  • 6
    Same as Drazen, had to use Admin mode + `bindingInformation=":8080:"` (without *) – Matthieu Charbonnier Jul 29 '15 at 13:36
  • 2
    This answer is correct, but outdated. VS 2015 stores the applicationhost.config in {solutiondir}\.vs\config – Adrian Grigore Jun 16 '16 at 12:01
  • @AdrianGrigore Thanks, I haven't used VS2015 much but when I do, I'll check it out. – Garrett Fogerlie Jun 16 '16 at 12:03
  • This worked for me. I replaced `localhost` with **(*)** – Rajiv Bhardwaj Apr 26 '17 at 07:57
  • 2
    `bindingInformation="*:49942:"` worked for me in vs 2015. Had to run as administrator otherwise it keep appending another new config for in `.vs/config/applicationhost.config` – resting Aug 23 '18 at 03:14
  • 1
    Duplicate the actual Binding line and add it directly underneath with the :port:* Make sure to run VS 'as admin', and not sure if it is new or not, but I had to enable SSL (which changed the port) so that a different computer on our network could actually access the site. – da_jokker Apr 23 '19 at 20:13
  • 2
    This worked for me. I'm using visual studio 2019 so my config file is located in my .vs folder and not in my documents. The rest of the procedure remained the same. – KoenW Feb 05 '20 at 19:16
  • 1
    It's not an acceptable security risk to run Visual Studio as administrator just for the purposes of doing some local development testing. See other answers that use the "netsh" command that allow external URLs in safely without the need to run as administrator. – Andy Mudrak May 18 '20 at 10:54
48

VisualStudio 2015 Non-Admin

  1. In your solution dir, in the file .vs\config\applicationHost.config change the line

    <binding protocol="http" bindingInformation="*:44302:localhost" />

    to

    <binding protocol="http" bindingInformation=":44302:" />

    (where 44302 is your port)


  1. From an admin command prompt:

    i. Enable non-admin to bind to port

    netsh http add urlacl url=http://*:44302/ user=Everyone

    ii. Allow through firewall

    netsh advfirewall firewall add rule name="IISExpress visualstudio app" protocol=tcp localport=44302 dir=in action=allow


  1. Start debugging from VisualStudio
gregmac
  • 24,276
  • 10
  • 87
  • 118
  • Note that .vs folder is hidden on windows 10. Show hidden files to see it. – bhathiya-perera Jun 23 '17 at 06:29
  • Oh! You are my hero! FINALLY! The key was the .vs folder's config. Can't thank you enough! – twsmale Jul 28 '18 at 20:27
  • Note as @ArunPrasadES mentiod below, run VS as administrator if you run into an error. – Elmer Apr 09 '20 at 15:40
  • @Elmer Running your IDE and debugging your programs as admin is a bad idea in general (with a few narrow exceptions). Most importantly, it masks permissions problems during development (when they're easily fixed), which eventually makes your program (unnecessarily) require being run as admin -- things like saving files in an admin-only location, etc. It's better to just fix the issue with your IDE and/or OS/user permissions. – gregmac Apr 10 '20 at 16:36
  • @gregmac Sorry for the bad comment what I meant was that my IIS had issues if I canged the binding options and not run VS as Admin, some process error ID, which I assume are related to Windows 10's permissions as I have a brand new Windows 10 installation with a new VS 2019 installation. As of the rest I agree with you 100% – Elmer Apr 14 '20 at 10:34
22

Except to modify the iisexpress configuration file, sometimes you also need to run the command like below.

netsh http add urlacl url=http://*:49419/ user=Everyone

shangkeyun
  • 231
  • 2
  • 3
15

How to avoid running Visual Studio as an administrator

Using both Garret's and @shangkeyun's answer you can achieve connecting to the running website without needing to run Visual Studio as an admin user:

  1. Open %USERPROFILE%\My Documents\IISExpress\config\applicationhost.config
  2. Search for your site using name=MySiteName
  3. Duplicate the existing <binding> item in the <bindings> section. You should now have two lines with binding.
  4. Remove the "localhost" part in bindingInformation.
  5. It should now look like this, assuming the port is 12345:

    <binding protocol="http" bindingInformation="*:12345:localhost" />
    <binding protocol="http" bindingInformation="*:12345:" />
    
  6. Enable non-admin to bind to port

    netsh http add urlacl url=http://*:12345/ user=Everyone
    

EDIT 2019: gregmac added a step to whitelist the VS instance. I never needed this, but listing it anyway:

  1. netsh advfirewall firewall add rule name="IISExpress visualstudio app" protocol=tcp localport=12345 dir=in action=allow
oligofren
  • 20,744
  • 16
  • 93
  • 180
10

Since I am unable to add a comment to @Garret Fogerlie's post and in response to the commenters' issue (@Y.Ecarri and @SamuelEdwinWard), I followed what Garret suggested, using Visual Studio 2013, running it in Admin mode and changing the application.config file.

After launching debug and seeing that I got the same error message, I went back into application.config and saw that a new entry for my site had been created just like Y.Ecarri's issue.

So I stopped debugging, kept my solution open in Visual Studio, and edited the application.config file again for the new entry. I also simply removed the * sings and localhost entirely, so I had the following for the new entry:

<binding protocol="https" bindingInformation=":44300:" />

johnnyRose
  • 7,310
  • 17
  • 40
  • 61
hvaughan3
  • 10,955
  • 5
  • 56
  • 76
2

Some of you might spend a lot of time modifying and testing using your %USERPROFILE% directory. If you are running on VS debug, use $(solutionDir).vs\config\applicationhost.config

carlo818
  • 209
  • 1
  • 3
  • 11
1

Thanks to byteit:

Go to applicationhost.config in Documents/IISExpress/config

find the entry for the particular site you are working on:

add:

<binding protocol="http" bindingInformation="*:<your site port>:*" />

in front of the existing

 <binding protocol="http" bindingInformation="*:<your site port>:localhost" />

To achieve the solution without having VS2013 create a new website xml entry for you when you restart. You will need to run as administrator.

Isaac Bolinger
  • 7,328
  • 11
  • 52
  • 90
1

After the above configurations, I had to run the Visual Studio in Administrative Mode.

enter image description here

Arun Prasad E S
  • 9,489
  • 8
  • 74
  • 87
0

This is what worked for me:

  • Start the IIS Manager
  • Add a new virtual directory that points to the projects folder (C:\VSProjects in my case)
  • Select the new virtual directory within IIS manager. Select Directory Browsing from the list of options. On the right side there's a Enable button. Click it.

Now I can access my folder and project bin on the network through mypcname\VSProjects\myProj\outputBinViewer.

SaiyanGirl
  • 16,376
  • 11
  • 41
  • 57
0

Had the a very similar issue debugging in Visual Studio Code, I solved it by adding:

"env": {
      // ...
      "ASPNETCORE_URLS": "http://*:5000" // change to the port you are using
      // ...
},

.. to launch.json

Apparently, by default it binds http protocol to 'localhost:5000', so it works with localhost but not with ip address - neither remotely nor locally.

If you are trying to hit a breakpoint by a request coming from a different computer, don't forget to check your firewall settings (and/or antivirus)

hope this helps

Carlos R Balebona
  • 1,916
  • 2
  • 15
  • 10