How to change default port no of the .NET core 6 API

How to change default port no of the .NET core 6 API

To change the default port number of a .NET Core 6 API, you can follow these steps:

  • Open the project in Visual Studio or your favorite code editor.

  • Locate the Program.cs file in the root directory of the project. This file contains the main entry point for the application.

  • Inside the CreateHostBuilder method, there is a call to the ConfigureWebHostDefaults method. This method sets up the default configuration for the web host, including the default port number.

  • To change the default port number, you can call the UseUrls method on the IWebHostBuilder object and pass in a new URL with the desired port number. For example, to change the default port number to 5000, you can add the following line of code:

.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseUrls("http://localhost:5000"); webBuilder.UseStartup<Startup>(); }); 
  • Save the changes to the Program.cs file and run the application. The application should now be listening on the new port number.

Alternatively, you can set the environment variable ASPNETCORE_URLS to the desired URL, which will override any URLs set in code. For example, to set the URL to http://localhost:5000, you can run the following command in your terminal:

export ASPNETCORE_URLS="http://localhost:5000" 

Note that if you want to use a port number lower than 1024, you may need to run the application with administrative privileges on some operating systems.

Examples

  1. "Change default port number in .NET Core 6 API"

    • Code:
      public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>().UseUrls("http://localhost:5001"); }); } 
    • Description: Modifies the default port number to 5001 for the .NET Core 6 API.
  2. ".NET Core 6 API set custom port number in appsettings.json"

    • Code:
      { "Kestrel": { "Endpoints": { "Http": { "Url": "http://localhost:6000" } } } } 
    • Description: Configures a custom port number (6000) for the Kestrel server in the appsettings.json file.
  3. "Change default port number dynamically in .NET Core 6 API"

    • Code:
      public class Program { public static void Main(string[] args) { var port = GetPortFromConfiguration(); // Implement a method to get the port number dynamically CreateHostBuilder(args, port).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args, int port) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>().UseUrls($"http://localhost:{port}"); }); } 
    • Description: Retrieves the port number dynamically from configuration and sets it during host configuration.
  4. "Customize port number for specific environment in .NET Core 6 API"

    • Code:
      public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>().UseUrls(GetUrlForEnvironment()); }); public static string GetUrlForEnvironment() { var port = Environment.GetEnvironmentVariable("ASPNETCORE_URLS_PORT") ?? "5002"; return $"http://localhost:{port}"; } } 
    • Description: Sets a custom port number based on the environment variable "ASPNETCORE_URLS_PORT."
  5. "ASP.NET Core 6 API change port number with command-line argument"

    • Code:
      public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>().UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build()); }); } 
    • Description: Changes the port number using a command-line argument during host configuration.
  6. ".NET Core 6 API use random available port"

    • Code:
      public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>().UseUrls("http://localhost:0"); }); } 
    • Description: Uses port 0 to dynamically assign an available port number at runtime.
  7. "Set HTTPS port number for .NET Core 6 API"

    • Code:
      public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>().UseUrls("https://localhost:5001"); }); } 
    • Description: Configures a custom port number (5001) for HTTPS in the .NET Core 6 API.
  8. "Use port number from environment variable in .NET Core 6 API"

    • Code:
      public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>().UseUrls($"http://localhost:{GetPortFromEnvironmentVariable()}"); }); public static string GetPortFromEnvironmentVariable() { return Environment.GetEnvironmentVariable("MY_API_PORT") ?? "5000"; } } 
    • Description: Retrieves the port number from the environment variable "MY_API_PORT" and uses it during host configuration.
  9. ".NET Core 6 API change port number with launchSettings.json"

    • Code:
      "profiles": { "MyApi": { "commandName": "Project", "launchBrowser": true, "applicationUrl": "http://localhost:5002", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } 
    • Description: Sets a custom port number (5002) in the launchSettings.json file for the Development environment.
  10. "Change port number in .NET Core 6 API for multiple environments"

    • Code:
      public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>().UseUrls($"http://localhost:{GetPortNumber()}"); }); public static string GetPortNumber() { var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"; return environment.ToLower() == "production" ? "80" : "5000"; } } 
    • Description: Sets different port numbers based on the environment (Production uses port 80, and others use port 5000).

More Tags

phpmyadmin extends mipmaps laravel-5.8 cmsamplebufferref webpack-dev-server monaco-editor at-command samsung-galaxy spacy

More C# Questions

More Genetics Calculators

More Housing Building Calculators

More Stoichiometry Calculators

More Chemical thermodynamics Calculators