Remotely connect to .net core self hosted web api

后端 未结 6 872
既然无缘
既然无缘 2020-12-29 02:11

I have a simple .net core web api with one action:

[Route(\"[action]\")]
public class APIController : Controller
{
    // GET api/values
    [HttpGet]
    pu         


        
相关标签:
6条回答
  • 2020-12-29 02:33

    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.

    0 讨论(0)
  • 2020-12-29 02:35

    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)

    0 讨论(0)
  • 2020-12-29 02:35

    There is a more accurate way when there are multi IP addresses available on the local machine. Connect a UDP socket and read its local endpoint:

    string localIP;
    using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
    {
        socket.Connect("8.8.8.8", 65530);
        IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
        localIP = endPoint.Address.ToString();
    }
    
    0 讨论(0)
  • 2020-12-29 02:41

    In my case (.NET core 2.1) I had to modify the Properties/launchSettings.json file.

    Set the applicationUrl to a list of allowed urls separated by semicolons like this

    "applicationUrl": "https://localhost:5001;http://odin:5000"
    

    Hope this helps someone out there.

    0 讨论(0)
  • 2020-12-29 02:51

    Another way to solve this problem is editing "applicationhost.config" file. in project folder -> .vs (Hidden folder) -> config open "applicationhost.config" file. Under sites section, site name="your project name" in Bindings node add another binding and change localhost with your "IP/Domain" on "bindingInformation", like This:

    <site name="project_name" id="2">
        <application path="/" applicationPool="Clr4IntegratedAppPool">
         <virtualDirectory path="/" physicalPath="D:\Projects\project_directory" />
        </application>
        <bindings>
         <binding protocol="http" bindingInformation="*:5000:localhost" />
         <binding protocol="http" bindingInformation="*:5000:192.168.1.2" />
         <binding protocol="http" bindingInformation="*:5000:odin" />
        </bindings>
    </site>
    

    remember Visual Studio must be run as Administrator.

    0 讨论(0)
  • 2020-12-29 02:55

    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.

    0 讨论(0)
提交回复
热议问题