0

I created a project using C# and ASP.NET Core empty and it worked on localhost - how can I access it from another device using the IP of the computer running the project?

enter image description here

enter image description here

enter image description here

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Share Code
  • 39
  • 3
  • Execute the `ipconfig` in command prompt of your system to know the IP address, and then use it on another system's browser as http:// 16x.n.n.n:5000 , if it is in same network it should work. If there is any firewall on your system that prevents inbound on non standard ports, you need to add a rule to allow port 5000 inbound traffic. – Anand Sowmithiran Feb 09 '22 at 05:47
  • @AnandSowmithiran I got the IPv4 address from `ipconfig` but when I access `http://my_ip:5000` I can't access it – Share Code Feb 09 '22 at 05:50
  • As suspected :-). See if you can reach to that ip from your device, using ping, and telnet kind of tools. Is it resolving and giving HTTP error code, or is it not resolving the IP addr itself? – Anand Sowmithiran Feb 09 '22 at 05:52
  • @AnandSowmithiran On the device running the project if I run `http://my_ip` I can access xampp website and I access `http://my_ip:5000` I can't access it, for external devices whether I access `http:my_ip` or `http://my_ip:5000` is not working – Share Code Feb 09 '22 at 05:57
  • You need to configure your xampp webserver as shown in this SO [answer](https://stackoverflow.com/questions/5524116/accessing-localhost-xampp-from-another-computer-over-lan-network-how-to/48990347#48990347) – Anand Sowmithiran Feb 09 '22 at 06:03
  • Does this answer your question? [Accessing IISExpress for an asp.net core API via IP](https://stackoverflow.com/questions/54500310/accessing-iisexpress-for-an-asp-net-core-api-via-ip) – Mohammad Aghazadeh Feb 09 '22 at 07:11
  • nothing answers the question, is not that simple. Please if you know give us some clue. Django works with same address, same port, why .NET cannot? – cikatomo May 16 '22 at 21:36

1 Answers1

3

You need to configure kestrel to bind requests from 0.0.0.0 instead of localhost to accept requests from everywhere.

You can achieve this with different methods.

1- Run application with urls argument

dotnet app.dll --urls "http://0.0.0.0:5000;https://0.0.0.0:5001"

2- Add kestrel config to appsettings.json (Or any other way that you can inject value to IConfiguration)

{
 ...
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://0.0.0.0:5000"
      },
      "Https": {
        "Url": "https://0.0.0.0:5001"
      }
    }
  }
  ...
}

3- Hard-code your binding in the program.cs file.

and the result should look like this

enter image description here

For complete details, please visit https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints

MrMoeinM
  • 1,728
  • 1
  • 9
  • 13