[英]Can't access WEB API with ip:port but can with localhost:port during VS debug mode
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.我的问题是我注意到该应用程序的 url 是localhost:port
并且它运行良好。 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
.但是,当我尝试将其更改为MYIP:port (eg. http:192.168.X.1234)
或MYHOSTNAME: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 上进行测试,而不是每次我想进行更改时都必须部署它?
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。
Both Anton and Matthew's Answers pointed me to the right direction安东和马修的回答都为我指明了正确的方向
So this what I did所以这就是我所做的
Run Visual Studios in administrator mode以管理员模式运行 Visual Studio
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更改了绑定协议并允许按照建议的传入方向http://johan.driessen.se/posts/Accessing-an-IIS-Express-site-from-a-remote-computer
But after that, I have a service unavailable (503) error但在那之后,我有一个服务不可用(503)错误
So I followed this : IIS Express Enable External Request - 503 Added just the port protocol and port:ip protocol,所以我遵循了这个: 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 用于从另一台机器访问)
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.当我想共享我的本地主机 IIS 时,我遇到了同样的问题,所以有些人只需输入我的机器名称或 IP 并连接到我的 Web 应用程序实例。 So if this is what you need when http://johan.driessen.se/posts/Accessing-an-IIS-Express-site-from-a-remote-computer .因此,如果这是您在http://johan.driessen.se/posts/Accessing-an-IIS-Express-site-from-a-remote-computer时需要的。 It works for me for both Silverlight and MVC apps.它适用于 Silverlight 和 MVC 应用程序。 I even set breakpoints and they are hit from a remote machine.我什至设置了断点,它们是从远程机器上命中的。
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.并以管理员身份运行 Visual Studio。 Restart IIS Express.重新启动 IIS Express。 this solution work for me.这个解决方案对我有用。
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 localhost but 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希望这可以帮助
I have been looking for this a while and finally found a solution for people who debug to console app (launch: project).我一直在寻找这个,终于为调试到控制台应用程序(启动:项目)的人找到了解决方案。
In the Program.cs class you have to add the UseUrls()在 Program.cs 类中,您必须添加 UseUrls()
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder
.UseUrls("http://0.0.0.0:5000", "https://0.0.0.0:5001")
.UseStartup<Startup>();
});
I couldnt find this anywhere online but I found it in some old project I had where it was working.我无法在网上的任何地方找到它,但我在它工作的一些旧项目中找到了它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.