Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access ASP.NET Core 2.1 web app hosted on localhost from mobile device

I create an out of the box asp.net core 2.1 web app with react. When I run it, it hosts on localhost:(some port). I want to access that on my mobile device.

I tried accessing it from localhost by adding the port to the Inbound Rules on Windows Defender Firewall.It didn't work so I wanted to change the ip address to 0.0.0.0:(some port) or to host it on my local IPv4 address by changing the lauchSettings.json and Program.cs by .UseUrls method. It didn't work, gave me an error that the app cannot run. I tried using my external IP address which I got from asking "My ip address" in Google but that didn't help as well.

Picture of Program.cs file

Picture of lauchSettings.js

Error when changing lauchSettings.js

like image 967
Andrea Avatar asked Nov 18 '18 18:11

Andrea


2 Answers

I use this, maybe it can help

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseUrls("https://*:5566")
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>();

Note that for the latest .Net, you set the URLs in the launchSettings.json file as shown below:

latest .Net version

Make sure to enable the port 5566 in firewall. From the phone access https://your_comp_ip_address:5566/ it should work. Change your_comp_ip_address to your computer ip address, you can check it from the CMD. Just run 'ipconfig' in the CMD

From Visual Studio, run the App itself, not IIS Express, select AppNamefrom the drop down, then press F5. Kindly Check all of these photos below:

From The Drop Down, select the AppName to run it

Windows Firewall Ports, add a new Outbound Rule: Port, say give a range of 5560-5570

Check the IP address

Make sure when you run, a CMD is displayed, and check out the code https://*:5566

From your phone browser, enter the url correctly

And then, voila!!! Your Web App is running

like image 153
Joseph Wambura Avatar answered Sep 29 '22 19:09

Joseph Wambura


In most cases, all you will have to do is replace "localhost" with a wildcard * (or with your local LAN IPv4 Address) in Properties/launchSettings.json:

{
  // ...
  "profiles": {
    // ...
    "Moshoboga": {
      // ...
      // replace: "applicationUrl": "https://localhost:5001;http://localhost:5000"
      "applicationUrl": "https://*:5001;http://*:5000"
    }
  }
}

Then navigate to your local ip in any browser on your LAN. Find this with ipconfig on Windows, or ifconfig on Unix/Linux

like image 30
Connor Low Avatar answered Sep 29 '22 19:09

Connor Low