I have a simple .net core web api with one action:
[Route("[action]")]
public class APIController : Controller
{
// GET api/values
[HttpGet]
public string Ping()
{
return DateTime.Now.ToString();
}
}
If I run this via dotnet run I get
Hosting environment: Production
Content root path: C:\Users\xxx\Documents\Visual Studio 2015\Projects\SelfHostTest\src\SelfHostTest
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
Going to the browser and typing in http://localhost:5000/ping results in a successful return of the current time. However going to a remote machine (same LAN) and trying to access the service via http://odin:5000/ping results in a 404 error. (Odin is the name of the machine running the web api in a console via dotnet run).
Both server (and client!) firewalls are turned off. I can ping "odin" successfully.
Any ideas what simple step I am missing here. I've tried this at home and at work with no success.
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.