0

I have my API which runs in localhost under port 8000. However I just want to know if is it okay to run my API on whatever port that I want (say port 1200,port 5000 etc). Or are there any specific ports among which only I can choose one to run my API. I'm new to this web development and hence this basic question.

1 Answers1

0

Yes it is okay! Just be sure to stay away from the common ports like MySQL (3306) and the rest.

Generally speaking, the 5000s are the ones most I've seen which .NET developers use. There are many ways to do so!

via launchSettings:

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:63902",
      "sslPort": 44305
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": false,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "Brevity.Api": {
      "commandName": "Project",
      "launchBrowser": false,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Or via Program.cs

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
        {
            return WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .UseKestrel(options =>
                {
                    options.AddServerHeader = false;

                    // options.Listen(IPAddress.Any, 8080);         // http:*:80

                    var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
                    var isDevelopment = environment == Environments.Development;
                    var validateSSL = !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("SSLCERT_PATH"));

                    // HTTPS Configuration
                    if (!System.Diagnostics.Debugger.IsAttached && !isDevelopment && validateSSL)
                    {
                        var hasHttpsPortConfigured = int.TryParse(Environment.GetEnvironmentVariable("HTTPS_PORT")
                            , out var httpsPort);
                        if (!hasHttpsPortConfigured)
                        {
                            httpsPort = 5001; // Default port

                            Console.WriteLine("HTTPS port not configured! Self configuring to 5001.");
                        }

                        var certPath = Environment.GetEnvironmentVariable("SSLCERT_PATH");

                        var certPassword = Environment.GetEnvironmentVariable("SSLCERT_PASSWORD");

                        options.Listen(IPAddress.Any, httpsPort, listenOptions =>
                        {
                            var cert = new X509Certificate2(certPath, certPassword);

                            listenOptions.UseHttps(cert);
                        });
                    }
                });
        }

Glad you're using .NET Core. We're just getting started to becoming one of the best languages to pick up programming!

Nicholas
  • 1,678
  • 13
  • 31
  • Thanks Nichoas. Could you please also clarify how to check all the available ports in my system – Krishna Sailesh Mar 26 '20 at 14:33
  • @KrishnaSailesh Hmm.. That's a little difficult! For mac, you can refer to https://stackoverflow.com/questions/4421633/who-is-listening-on-a-given-tcp-port-on-mac-os-x. Depends on an individual's OS. – Nicholas Mar 26 '20 at 16:09