Reputation: 14145
my simple web app has web api and web mvc project. from mvc app I'm consuming web api service using ajax.
Right now my uri looks like this
var uri = 'http://localhost:2266/api/cars';
and on web api side I need to enable cors for communication with my mvc app.
[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?\
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?
Upvotes: 2
Views: 1506
Reputation: 12351
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").
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).
"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.
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; if they are completely separate applications (as it seems), then you're probably better off doing same on live - e.g. 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
).
Hth.
Upvotes: 2
Reputation: 6891
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.
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.
The host entry would look like 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.
Your friend also need to enter the host entry on his machine with the IP of your machine.
The host entry would look like <<your computer IP>> apionlocal.com
Your friend need to call API using the domainname "apionlocal.com".
You can configure multiple origins in "EnableCors" using comma separated values.
EnableCors(origins: "http://localhost:2256,<<Url of your friend's application>>", headers: "*", methods: "*")]
Upvotes: 0
Reputation: 6151
You can separate multiple origins with commas:
[EnableCors(origins: "http://localhost:2256,http://localhost:2266", headers: "*", methods: "*")]
From MSDN:
Parameters origins Type: System.String Comma-separated list of origins that are allowed to access the resource. Use "*" to allow all.
Upvotes: 0