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

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

问题描述

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

无法连接到Web服务器"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连接到RESTful API.Forms移动应用程序(以及同一网络上的其他计算机)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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