Remotely connect to .net core self hosted web api

Solution 1:

My guess is that the issue isn't in your controller, it is in program.cs. You need to modify the construction of your WebHost

var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseUrls("http://localhost:5000", "http://odin:5000", "http://192.168.1.2:5000")
.UseIISIntegration()
.UseStartup<Startup>()
.Build();

Unless you add the UseUrls line, Kestrel isn't going to listen outside of localhost. This makes sense, because in a normal situation Kestrel will be sitting behind a reverse proxy like IIS or NGNIX and doesn't need to bind to external URLs.

Solution 2:

The best way is to adjust the launchSettings.json, which is located inside the Properties folder.

Change

"applicationUrl": "https://localhost:5001"

to

"applicationUrl": "https://0.0.0.0:5001"

This allows the Kestrel Web Server to listen for traffic from all Network Interfaces.

Solution 3:

You can simply do the following to create your WebHost, this will allow remote connections to kestrel.

var host = WebHost.CreateDefaultBuilder(args)
                .UseUrls("http://0.0.0.0:80")
                .UseStartup<Startup>()
                .Build();

After using the following code I still wasn't able to access my API remotely, I had to disable the network adapters created by Docker in the windows control panel (Control Panel\Network and Internet\Network Connections)