I am writing an ASP.NET Core API that I want to access on my internal network, how can I make it internally accessible?
I tried following the steps on this thread: Remotely connect to .net core self hosted web api
But I am still not able to access it.
This is the code for my Program.cs:
public class Program
{
public static void Main(string[] args)
{
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
var ip = GetLocalIPAddress();
var url = $"http://{ip}:5000";
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseUrls("http://localhost:5000", url)
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
public static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("No network adapters with an IPv4 address in the system!");
}
}
Simple endpoint that I am trying to reach:
[Route("api/heartbeat")]
[ApiController]
public class HeartbeatController : ControllerBase
{
[HttpHead]
public IActionResult Head()
{
return Ok();
}
[HttpGet]
public IActionResult Get()
{
var ip = GetLocalIPAddress();
return Ok($"Success ~ Api started on : http://{ip}:5000");
}
private static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("No network adapters with an IPv4 address in the system!");
}
}