C# 无法使用 ip:port 访问 WEB API,但在 VS 调试模式下可以使用 localhost:port
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19554996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't access WEB API with ip:port but can with localhost:port during VS debug mode
提问by noobiehacker
I am trying to write an WEB API from .net and trying for my Android Application to query some data from the sql server database.
我正在尝试从 .net 编写一个 WEB API,并尝试让我的 Android 应用程序从 sql server 数据库中查询一些数据。
I have the web api written and it works well in debug mode.
我编写了 web api,它在调试模式下运行良好。
My question is I notice the url of that application is localhost:port
and it runs fine.
However, when I try to change it to MYIP:port (eg. http:192.168.X.1234)
or MYHOSTNAME:port (eg win7home:1234)
this gives me Bad Request - Invalid Hostname
.
我的问题是我注意到该应用程序的 url 是localhost:port
并且运行良好。但是,当我尝试将其更改为MYIP:port (eg. http:192.168.X.1234)
orMYHOSTNAME:port (eg win7home:1234)
这给了我Bad Request - Invalid Hostname
.
I know I can deploy this to IIS and my IIS is setup but I was just wondering how come it doesn't work in debug mode???
我知道我可以将它部署到 IIS 并且我的 IIS 已设置,但我只是想知道它为什么在调试模式下不起作用???
Is there a way for me to run it in debug mode and test in on my Android at the same time instead of having to deploy it every time I want to make a change?
有没有办法让我在调试模式下运行它并同时在我的 Android 上进行测试,而不是每次我想进行更改时都必须部署它?
采纳答案by noobiehacker
Both Anton and Matthew's Answers pointed me to the right direction
安东和马修的回答都为我指明了正确的方向
So this what I did
所以这就是我所做的
Run Visual Studios in administrator mode
Changed the binding protocols and allow for incoming directions as suggested http://johan.driessen.se/posts/Accessing-an-IIS-Express-site-from-a-remote-computer
But after that, I have a service unavailable (503) error
So I followed this : IIS Express Enable External Request - 503Added just the port protocol and port:ip protocol,
以管理员模式运行 Visual Studio
更改了绑定协议并允许按照建议的传入方向 http://johan.driessen.se/posts/Accessing-an-IIS-Express-site-from-a-remote-computer
但在那之后,我有一个服务不可用(503)错误
所以我遵循了这个: IIS Express Enable External Request - 503只添加了端口协议和端口:ip 协议,
Than it works both on my machine's browser and on my phone.
比它在我机器的浏览器和我的手机上都有效。
Not too too sure why the 3rd step is needed -my hypothesis is (the localhost url is needed for VS to point to and the ip url is used for accessing from another machine)
不太确定为什么需要第三步 - 我的假设是(VS 需要 localhost url 指向,并且 ip url 用于从另一台机器访问)
回答by Matthew
If you're running it in debug mode I assume you're using IIS-Express.
如果您在调试模式下运行它,我假设您使用的是 IIS-Express。
By default, IIS-Express only binds to localhost
.
默认情况下,IIS-Express 只绑定到localhost
.
To circumvent this, you can open the IIS-Express application config file located at: C:\Users\<username>\My Documents\IISExpress\config\applicationhost.config
and modify the site's binding information.
为了避免这种情况,您可以打开位于以下位置的 IIS-Express 应用程序配置文件:C:\Users\<username>\My Documents\IISExpress\config\applicationhost.config
并修改站点的绑定信息。
change
改变
<binding protocol="http" bindingInformation="*:55284:localhost" />
to
到
<binding protocol="http" bindingInformation="*:55284:*" />
You'll also have to restart IIS-Express after the change.
您还必须在更改后重新启动 IIS-Express。
回答by Anton
I had the same problems when I wanted to share my localhost IIS so some guys could just type my machine name or IP and connect to my web app instance. So if this is what you need when http://johan.driessen.se/posts/Accessing-an-IIS-Express-site-from-a-remote-computer. It works for me for both Silverlight and MVC apps. I even set breakpoints and they are hit from a remote machine.
当我想共享我的 localhost IIS 时,我遇到了同样的问题,所以有些人只需输入我的机器名称或 IP 并连接到我的 Web 应用程序实例。因此,如果这是您在http://johan.driessen.se/posts/Accessing-an-IIS-Express-site-from-a-remote-computer时需要的。它适用于 Silverlight 和 MVC 应用程序。我什至设置了断点,它们是从远程机器上命中的。
回答by Carlos R Balebona
Had the same issue debugging in Visual Studio Code, I solved it by adding:
在Visual Studio Code 中调试时遇到了同样的问题,我通过添加以下内容解决了它:
"env": {
// ...
"ASPNETCORE_URLS": "http://*:5000" // change to the port you are using
// ...
},
.. to launch.json
..启动.json
Apparently, by default it binds http protocol to 'localhost:5000', so it works with localhostbut not with ip address.
显然,默认情况下,它将 http 协议绑定到 'localhost:5000',因此它适用于localhost但不适用于ip address。
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
希望这可以帮助
回答by Ahmad Aghazadeh
In your solution dir, in the file .vs\config\applicationHost.config change the line
在您的解决方案目录中,在文件 .vs\config\applicationHost.config 中更改行
<binding protocol="http" bindingInformation="*:1234:localhost" />
to
到
<binding protocol="http" bindingInformation=":1234:192.168.1.35" />
and run Visual Studio as an admin. Restart IIS Express. this solution work for me.
并以管理员身份运行 Visual Studio。重新启动 IIS Express。这个解决方案对我有用。