从 Xamarin.Forms 移动应用程序(以及同一网络上的其他计算机)连接到 RESTful API [英] Connecting to a RESTful API from Xamarin.Forms mobile application (and other computers on the same network)

查看:16
本文介绍了从 Xamarin.Forms 移动应用程序(以及同一网络上的其他计算机)连接到 RESTful API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的 Xamarin.Forms 移动应用程序连接到我在本地计算机上托管的 RESTful API.我将 Visual Studio 用于 Xamarin.Forms 移动应用程序和 API,对于 API,我使用的是 ASP.NET Core(Visual Studio 中的特定模板是 ASP.NET Core Web Application (.NET Framework) ).
当我在运行 API 的同一台计算机上使用本地主机地址从浏览器连接到它时,它工作正常(在我的情况下,localhost:59475/api/是 URL)并且在将我的移动应用程序连接到之前API,我首先尝试从同一网络上的其他计算机连接到 API,但到目前为止我没有设法让它工作.到目前为止我尝试的是以下 - 运行 cmd 命令 netsh http add urlacl url=http://*:59475/user=everyone (以及我认为可能工作的其他端口的其他类似命令),然后我添加了一个入站防火墙规则以允许该端口上的所有连接.然后,我尝试通过将地址中的localhost"替换为运行 API 的计算机的 IP 来从网络上的其他计算机连接到 API,并且在执行此操作时收到了 HTTP 503 错误.Evantually 发生的事情是,当我尝试从 Visual Studio 运行 API 时,它显示了

I am trying to connect my Xamarin.Forms mobile application to a RESTful API I'm hosting locally on my computer. I'm using Visual Studio for both the Xamarin.Forms mobile application and for the API, and for the API, I'm using ASP.NET Core (the specific template in Visual Studio is ASP.NET Core Web Application (.NET Framework) ).
It worked fine when I connected to it from my browser using a localhost address on the same computer I ran the API from (in my case, localhost:59475/api/ was the URL) and before connecting my mobile application to the API, I first tried to connect to the API from other computers on the same network, and I didn't manage to make it work so far. What I tried so far is the following - run the cmd command netsh http add urlacl url=http://*:59475/ user=everyone (along with other similar commands with other ports I thought may work), then I added an inbound firewall rule to allow all connections on that port. I then tried to connect to the API from other computers on the network by replacing "localhost" in the address with the IP of the computer running the API, and I received an HTTP 503 error when I did. Evantually what happend is when I tried to run the API from Visual Studio it showed an error of

无法连接到网络服务器IIS Express"

Unable to connect to web server 'IIS Express'

撤消"netsh http 命令(netsh http delete urlacl...)使它再次像最初那样工作(同样,只能使用我的浏览器从同一台计算机连接到它).我不知道如何使它工作,希望得到帮助!:)

"Undoing" the netsh http commands (netsh http delete urlacl...) made it work again as it did at first (again, only able to connect to it from the same computer using my browser). I don't know how to make it work and would like to receive help please! :)

推荐答案

尝试以下操作:

  1. 显式添加您的外部 IP netsh http add urlacl url=http://192.168.0.6:60985/user=everyone

在 API 解决方案文件夹的根目录中打开.vs/config/applicationhost.config"文件(.vs 默认隐藏)

In the root of the API solution folder open the '.vs/config/applicationhost.config' file (.vs is hidden by default)

在绑定部分添加一个新的绑定到外部地址<binding protocol="http" bindingInformation="*:60985:192.168.0.6"/>,注意您要添加绑定的站点,因为可能有多个.

in the bindings section add a new binding to the external address <binding protocol="http" bindingInformation="*:60985:192.168.0.6" />, mind to which site you are adding the bindings as there could be multiple.

注意:示例端口 60985 您的可能不同,请检查 api示例IP:192.168.0.6,改为你实际的外部IP地址

Notes: example port 60985 yours could be different check the api example IP: 192.168.0.6, change to your actual external IP address

您可能希望禁用防火墙进行测试,然后在确认其正常工作时启用它并添加例外规则

You might want to disable the firewall for testing then when you confirm it works, enable it and add an exception rule

这篇关于从 Xamarin.Forms 移动应用程序(以及同一网络上的其他计算机)连接到 RESTful API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆