5

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
  • 732,580
  • 175
  • 1,330
  • 1,459
Share Code
  • 89
  • 1
  • 4
  • 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
  • 1
    @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
  • you need a binding to ip address in your application , so web server (depends which one you using) kestrel, iis... will know how to redirect requests. – Power Mouse Mar 22 '23 at 19:05

3 Answers3

15

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://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints

MrMoeinM
  • 2,080
  • 1
  • 11
  • 16
  • 2
    not working for me on .NET 6 , windows 10 – cikatomo May 16 '22 at 21:43
  • Worked for me on Asp .Net Core 6 , MacOS. – Isu Aug 26 '22 at 16:10
  • I receive the below error on iPhone when changing from https://localhost:7139 to https://0.0.0.0:7139. It worked on my laptop (where LiveServer was running) `Failed to load resource: The certificate for this server is invalid. You might be connecting to a server that is pretending to be “192.168.0.10”, which could put your confidential information at risk.` – Ollie Jan 01 '23 at 20:37
  • 1
    @Ollie use http instead on https. Your certificate is only valid on localhost. – MrMoeinM Jan 02 '23 at 07:32
  • 1
    it worked for me. Thank you very much. I've been looking for an answer for hours. ASP NET CORE 3.1 – BREI Jan 13 '23 at 12:27
0

As an alternative and easier option for access from other devices, you can use Ngrok. It will make your localhost accessible and public from the outside and for other devices. Setting up and installing everything you need will take about 5 minutes. If you suddenly don’t figure out how to set up Ngrok, I attach a video tutorial step by step

0

For .Net 6.0 API, in launchsettings.json, I changed all the localhost values to 0.0.0.0.shot of launchsettings showing 0.0.0.0 instead of localhost

Mark G
  • 557
  • 1
  • 6
  • 17