Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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:UsersxxxDocumentsVisual Studio 2015ProjectsSelfHostTestsrcSelfHostTest
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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
496 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...