Bad Request - Invalid Hostname ASP.net Visual Studio 2015
Asked Answered
S

8

43

After debugging my website in Visual Studio 2015, I can visit it via localhost:50544. I would like to visit my website on a different computer from which it is being served upon that is also on the same network. To do this I should be able to visit that computers address which is 192.168.1.13:50544.

However when visiting this address I get a 'Bad request, invalid host name' error. Even if I visit it on the same machine as the website is being served on.

Following the advice here I have created the following windows firewall rule and have also tried turning the firewall off entirely. picture of firewall rule

I'm using IIS express and so have added to the '~\Documents\IISExpress\config\applicationhost.config' file

<binding protocol="http" bindingInformation=":8080:localhost" /> //original rule
<binding protocol="http" bindingInformation="*:50544:192.168.1.13" />

But visiting 192.168.1.13:50544 on any machine still results in the 'Bad Request' error.

Silica answered 6/2, 2016 at 23:33 Comment(1)
Also noteworthy is that there are many applicationhost.config files. I were editing the file located in %USERPROFILE%\Documents\IISExpress\config when you actually edit the one in ProjectName\.vs\configSilica
S
32

The format of the bindingInformation attribute is:

ip address:port:host header

What you need is:

bindingInformation="192.168.1.13:50544:*"

Source: https://www.iis.net/configreference/system.applicationhost/sites/site/bindings/binding?showTreeNavigation=true#006

Update:

There appear to be some folks who are under the mistaken belief that this answer is incorrect. First of all go read the documentation (see link above) and read the examples.

Secondly IIS and IIS Express are essentially the same product. IIS Express has been tweaked so that non-administrators can run IIS on developer PC's where they might not have full local admin rights. Despite this, both IIS and IIS Express use the same applicationHost.config file formats, and this means that the bindingInformation attribute format is exactly the same for both products.

The reason that the (incorrect) :50544:192.168.1.13 bindingInformation string works is because (rightly or wrongly):

  • if there is no host header specified

  • if there is no matching IP address in the first part of the binding info field

... then IIS will try to match to an IP address in the host header part of the binding info string.

The correct format for both IIS7+ and IIS Express is, and always has been:

ip_address:port:host_header
Sophronia answered 7/2, 2016 at 5:17 Comment(13)
This is not true for VS 2015 / IIS10. <binding protocol="http" bindingInformation="*:50544:192.168.1.13" /> is correct.Undercut
@Undercut I would beg to differ. The format is and always has been ip_address:port:host_header. Just checked my own IIS Express and Full Fat IIS, See also: iis.net/configreference/system.applicationhost/sites/site/…Sophronia
Why do you think it's different on your machine?Sophronia
@Undercut - please see my update to this answer which explains why *:50544:192.168.1.13 happens to work, but is wrong.Sophronia
Please have a look at official Microsoft documentation: msdn.microsoft.com/en-us/library/…Undercut
Also note whenever I try to write bindingInformation your way, it is automatically DELETED and REWRITTEN.Undercut
@Undercut - how are you doing this - via the MWA API? Editing applicationhost directly? via the binding manager in IIS Manager MMC? I looked at that documentation link, it's exactly as I described in my answer: ip:port:host.Sophronia
OK, now I read back and understand what you mean. It's passed a long time and I am not really that much more into it. So, it is upside down, the system is automatically rewriting my applicationhost bindingInformation to "port:ip" format, no matter how I write it.Undercut
@Undercut - sounds like you were missing either a leading or trailing :. What did you use to set the binding info string?Sophronia
Sorry mate, I don't remember it was 2 months ago. It is not that important. I just remarked my different personal experience in that circumstance. Have a nice day!Undercut
Thanks a lot @Sophronia was struggling with this a lot. your answer sloved it.Cadmann
In my case, adding :* made VS crash, but it works if I delete the colon and asterisk at the end.Ankus
VS2019: changing the existing binding for localhost port 2916 worked with <binding protocol="http" bindingInformation="*:2916:*" />Koph
R
46

You have to run Visual Studio as Administrator

In Visual Studio 2015 the applicationhost.config is located it the folder in the project itself. Esc MyProjectName\.vs\config, note that the .vs folder may be hidden. Change the Ip from there, not IISExpress folder.

Your <binding protocol="http" bindingInformation="*:50544:192.168.1.13" /> is correct.

Redintegration answered 27/1, 2017 at 14:1 Comment(5)
Run as Admin is key here!Impart
Didn't work for me. Windows 10, VS15, IIS Express serving https (may that be the problem?). Following instructions. Run as Administrator ecc... Changing the IIS config results in it be automatically overwritten by adding an entire new section "site name='mywebsite(x)'. with 'x' as a progressive int and with old "localhost" address any time I start the website. Modifying from VS15 (website properties) web > project url: "192.168.0.2:44324/"; results in "Unable to launch the IIS Express web server".Undercut
Not work ... s17.postimg.org/43uuusdrz/chrome_2017-05-22_22-17-20.png help me.Galsworthy
@MatheusMiranda You have to use Internet Information Services (IIS) Manager instead. [link] (i-technet.sec.s-msft.com/dynimg/IC396222.jpg)Redintegration
@Chaika - this answer is wrong. See my updated answer which explains why this works, but is the incorrect solution.Sophronia
S
32

The format of the bindingInformation attribute is:

ip address:port:host header

What you need is:

bindingInformation="192.168.1.13:50544:*"

Source: https://www.iis.net/configreference/system.applicationhost/sites/site/bindings/binding?showTreeNavigation=true#006

Update:

There appear to be some folks who are under the mistaken belief that this answer is incorrect. First of all go read the documentation (see link above) and read the examples.

Secondly IIS and IIS Express are essentially the same product. IIS Express has been tweaked so that non-administrators can run IIS on developer PC's where they might not have full local admin rights. Despite this, both IIS and IIS Express use the same applicationHost.config file formats, and this means that the bindingInformation attribute format is exactly the same for both products.

The reason that the (incorrect) :50544:192.168.1.13 bindingInformation string works is because (rightly or wrongly):

  • if there is no host header specified

  • if there is no matching IP address in the first part of the binding info field

... then IIS will try to match to an IP address in the host header part of the binding info string.

The correct format for both IIS7+ and IIS Express is, and always has been:

ip_address:port:host_header
Sophronia answered 7/2, 2016 at 5:17 Comment(13)
This is not true for VS 2015 / IIS10. <binding protocol="http" bindingInformation="*:50544:192.168.1.13" /> is correct.Undercut
@Undercut I would beg to differ. The format is and always has been ip_address:port:host_header. Just checked my own IIS Express and Full Fat IIS, See also: iis.net/configreference/system.applicationhost/sites/site/…Sophronia
Why do you think it's different on your machine?Sophronia
@Undercut - please see my update to this answer which explains why *:50544:192.168.1.13 happens to work, but is wrong.Sophronia
Please have a look at official Microsoft documentation: msdn.microsoft.com/en-us/library/…Undercut
Also note whenever I try to write bindingInformation your way, it is automatically DELETED and REWRITTEN.Undercut
@Undercut - how are you doing this - via the MWA API? Editing applicationhost directly? via the binding manager in IIS Manager MMC? I looked at that documentation link, it's exactly as I described in my answer: ip:port:host.Sophronia
OK, now I read back and understand what you mean. It's passed a long time and I am not really that much more into it. So, it is upside down, the system is automatically rewriting my applicationhost bindingInformation to "port:ip" format, no matter how I write it.Undercut
@Undercut - sounds like you were missing either a leading or trailing :. What did you use to set the binding info string?Sophronia
Sorry mate, I don't remember it was 2 months ago. It is not that important. I just remarked my different personal experience in that circumstance. Have a nice day!Undercut
Thanks a lot @Sophronia was struggling with this a lot. your answer sloved it.Cadmann
In my case, adding :* made VS crash, but it works if I delete the colon and asterisk at the end.Ankus
VS2019: changing the existing binding for localhost port 2916 worked with <binding protocol="http" bindingInformation="*:2916:*" />Koph
U
10

We made an extension called Conveyor that you can use to open up IIS Express to external access without any config changes.

https://marketplace.visualstudio.com/items?itemName=vs-publisher-1448185.ConveyorbyKeyoti

Ungodly answered 8/3, 2018 at 5:26 Comment(3)
none of the other answers here worked for me, but this was an amazingly simple solution that worked the first time. Thanks!Macrophysics
Works like a charmPhosphate
I don't really like installing extensions, but this actually does work, so thanks!Boynton
A
4

In case someone is still having issues with this, the thing that fixed it for me is running Visual Studio in administrator mode.

I've read somewhere that IIS will not let you use anything else than localhost as the host if not in that mode (after editing the config file I was getting error that IIS could not be started)

Also very important as stated by OP, edit the file in your project (%PROJECT%\.vs\%PROJECT%\config\applicationhost.config), not the one in the IIS Express folder...

Adjudge answered 22/12, 2019 at 18:21 Comment(0)
N
1

This solution helped me with .net 6 aspNetCore app.

Find a file Properties\launchSettings.json

Go to iisSettings->iisExpress->applicationUrl

I replaced my http://localhost:51222/ to http://192.168.1.126:51222/

"iisExpress": {
  "applicationUrl": "http://192.168.1.126:51222/",
  "sslPort": 44392
}
Nectar answered 25/11, 2021 at 0:11 Comment(0)
A
0

I had same issue with remote access and I tried all of these tips to fix it. For me helped ticking "Allow anonymous authentication" in Web project config.

Ait answered 20/4, 2018 at 8:40 Comment(0)
M
0

I had everything working fine until today.

  • firewall ok
  • VS runs in admin mode
  • applicationhost.config file ok

I just disabled "Enable Edit and continue" (in csproj) and it stopped working. I enabled it again, and there we go again.

Hope this helps

Mabuse answered 10/10, 2019 at 5:44 Comment(0)
S
0

I fix this by enable it globally thru my network, not only in specific IP.

From CMD(need to run as admin)

netsh http add iplisten ipaddress=0.0.0.0

Inside project applicationhost.config I put the binding globally as below:

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

This will make your application can be accessed from all machine with the same network. Do note also for me I need to run visual studio as admin as well. It may not be true for your case.

Sammer answered 16/1 at 6:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.