简体   繁体   English

使用 web api,不同机器上的端口

[英]consuming web api, ports on different machine

my simple web app has web api and web mvc project.我的简单 web 应用程序有 web api 和 web mvc 项目。 from mvc app I'm consuming web api service using ajax.从 mvc 应用程序我正在使用 ajax 使用 web api 服务。

Right now my uri looks like this现在我的 uri 看起来像这样

var uri = 'http://localhost:2266/api/cars';

and on web api side I need to enable cors for communication with my mvc app.在 web api 端,我需要启用 cors 以与我的 mvc 应用程序通信。

[EnableCors(origins: "http://localhost:2256", headers: "*", methods: "*")]

this setup working just fine on my machine, since I want to share this with my friend how can I make this ports values 2256 and 2266 to be same on each machine?\\这个设置在我的机器上运行得很好,因为我想与我的朋友分享这个,我怎样才能使每台机器上的端口值22562266相同?\\

Or if it's not what do you suggest to make this values configurable?或者,如果这不是您建议使这些值可配置的建议?

Thank you, but obviously I was not clear enough: I'm asking how can I know on different computer which port should be used, and upon that to include that port instead of this 2256, 2266?谢谢,但显然我不够清楚:我问我如何知道在不同的计算机上应该使用哪个端口,并在此基础上包含该端口而不是 2256、2266?

The port number is just the tooling (VS) making it easy for you to debug locally ( localhost ) - it "separates" each of your web sites by assigning a random port number all living in your local machine under 'localhost` (aka "domain").端口号只是工具 (VS) 使您可以轻松地在本地调试 ( localhost ) - 它通过分配一个随机端口号来“分隔”您的每个网站,这些端口号都位于“localhost”下的本地计算机中(又名“领域”)。

You can in fact assign one yourself (or change whatever VS assigns) with the caveat of making sure it doesn't conflict with any other service/existing port in use in your local machine (more below).实际上,您可以自己分配一个(或更改 VS 分配的任何内容),但要注意确保它不与本地计算机中使用的任何其他服务/现有端口冲突(更多信息见下文)。

"share this with my friend" “与我的朋友分享这个”

Unsure what that means.不确定这意味着什么。 As above localhost is local , so assuming on the (live/production) net - then you assign a port number (in your server/IIS settings) - it's your API so you "dictate" how external "friends" can access/connect.如上所述localhostlocal ,因此假设在(实时/生产)网络上-然后分配一个端口号(在您的服务器/IIS设置中)-这是您的API,因此您“决定”外部“朋友”如何访问/连接。

Caveats:注意事项:

As mentioned above, just like in your local environment (which is less restrictive being local), in production/live internet, aside from the standard ports (80, 443, 20, 21, 22, 25, etc.) you may run into issues with firewalls, which ports to use (conflict with other existing services), etc. which is a broad topic and frankly its been a long time since I've done net ops - TLDR;如上所述,就像在您的本地环境中(本地限制较少)一样,在生产/实时互联网中,除了标准端口(80、443、20、21、22、25 等)之外,您可能会遇到防火墙问题,使用哪些端口(与其他现有服务冲突)等,这是一个广泛的话题,坦率地说,自从我完成网络操作以来已经很长时间了 - TLDR; if they are completely separate applications (as it seems), then you're probably better off doing same on live - eg subdomains - www.foo.com (mvc) api.foo.com (api) and not deal with assigning port numbers manually at all (defaults will apply - 80 for http , 443 for https ).如果它们是完全独立的应用程序(看起来如此),那么您可能最好在实时 - 例如子域 - www.foo.com (mvc) api.foo.com (api) 上做同样的事情,而不是处理分配端口号完全手动(默认将适用 - http80https443 )。

Hth.嗯。

You can separate multiple origins with commas:您可以用逗号分隔多个来源:

[EnableCors(origins: "http://localhost:2256,http://localhost:2266", headers: "*", methods: "*")]

From MSDN :MSDN

Parameters origins Type: System.String Comma-separated list of origins that are allowed to access the resource.参数origins类型:System.String 逗号分隔的允许访问资源的源列表。 Use "*" to allow all.使用“*”允许所有。

The ports which are using for your Web API application hosting will not be accessible outside of your computer and also it might get changed when you close and open Visual Studio and run Web API again.用于您的 Web API 应用程序托管的端口将无法在您的计算机外部访问,并且当您关闭并打开 Visual Studio 并再次运行 Web API 时,它也可能会更改。

It would be best to host your Web API using a domainname (such as "apionlocal.com") on local machine and create a host entry on the local machine.最好在本地机器上使用域名(例如“apionlocal.com”)托管您的 Web API,并在本地机器上创建一个主机条目。

The host entry would look like 127.0.0.1 apionlocal.com主机条目看起来像127.0.0.1 apionlocal.com

Retrieve netrowk IP of your computer by running "ipconfig" command on command prompt and share it with your friend.通过在命令提示符下运行“ipconfig”命令来检索您计算机的 netrowk IP 并与您的朋友分享。

Your friend also need to enter the host entry on his machine with the IP of your machine.您的朋友还需要使用您机器的 IP 在他的机器上输入主机条目。

The host entry would look like <<your computer IP>> apionlocal.com主机条目看起来像<<your computer IP>> apionlocal.com

Your friend need to call API using the domainname "apionlocal.com".您的朋友需要使用域名“apionlocal.com”调用 API。

You can configure multiple origins in "EnableCors" using comma separated values.您可以使用逗号分隔值在“EnableCors”中配置多个源。

EnableCors(origins: "http://localhost:2256,<<Url of your friend's application>>", headers: "*", methods: "*")]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM