How to change the default port in asp.net Core 3 or Net Core 5

How to change the default port in asp.net Core 3 or Net Core 5

To change the default port in ASP.NET Core 3 or .NET Core 5, you need to modify the CreateHostBuilder method in the Program class of your project. By default, this method configures Kestrel, the web server used by ASP.NET Core, to listen on port 5000 for HTTP traffic and port 5001 for HTTPS traffic.

To change the default port, you can call the UseUrls() method on the IWebHostBuilder object returned by CreateHostBuilder() and specify the new port number(s) as a string. Here's an example of how to modify CreateHostBuilder to listen on port 8000:

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

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.UseUrls("http://*:8000");
                webBuilder.UseStartup<Startup>();
            });
}

In this example, the UseUrls() method is called with the argument "http://*:8000", which specifies that Kestrel should listen on all IP addresses (*) and port 8000 for HTTP traffic.

Note that you can also specify multiple URLs by passing a comma-separated list of URLs as the argument to UseUrls(), like this:

webBuilder.UseUrls("http://*:8000,https://*:8001");

This would configure Kestrel to listen on port 8000 for HTTP traffic and port 8001 for HTTPS traffic, for all IP addresses.

Examples

  1. "Change default port in ASP.NET Core 3"

    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>();
                    webBuilder.UseUrls("http://localhost:5001"); // Change to desired port
                });
    }
    

    Description: In the Program.cs file, you can use UseUrls to specify the desired port when configuring the web host.

  2. "Change default port in .NET Core 5"

    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>();
                    webBuilder.UseUrls("http://localhost:5001"); // Change to desired port
                });
    }
    

    Description: Similar to ASP.NET Core 3, in .NET Core 5, you can use UseUrls in the Program.cs file to set the desired port.

  3. "ASP.NET Core 3 change default port in appsettings.json"

    Code:

    {
      "Kestrel": {
        "EndPoints": {
          "Http": {
            "Url": "http://localhost:5001" // Change to desired port
          }
        }
      }
    }
    

    Description: In the appsettings.json file, you can specify the desired port under the Kestrel configuration in ASP.NET Core 3.

  4. ".NET Core 5 change default port in appsettings.json"

    Code:

    {
      "Kestrel": {
        "EndPoints": {
          "Http": {
            "Url": "http://localhost:5001" // Change to desired port
          }
        }
      }
    }
    

    Description: Similar to ASP.NET Core 3, in .NET Core 5, you can specify the desired port under the Kestrel configuration in the appsettings.json file.

  5. "Change default port in ASP.NET Core 3 launchSettings.json"

    Code:

    {
      "iisSettings": {
        "iisExpress": {
          "applicationUrl": "http://localhost:5001", // Change to desired port
          "sslPort": 0
        }
      },
      "profiles": {
        "YourAppName": {
          "commandName": "Project",
          "launchBrowser": true,
          "applicationUrl": "http://localhost:5001", // Change to desired port
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }
    

    Description: In the launchSettings.json file, you can specify the desired port under the applicationUrl for both IIS Express and your application profile in ASP.NET Core 3.

  6. ".NET Core 5 change default port in launchSettings.json"

    Code:

    {
      "iisSettings": {
        "iisExpress": {
          "applicationUrl": "http://localhost:5001", // Change to desired port
          "sslPort": 0
        }
      },
      "profiles": {
        "YourAppName": {
          "commandName": "Project",
          "launchBrowser": true,
          "applicationUrl": "http://localhost:5001", // Change to desired port
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }
    

    Description: Similar to ASP.NET Core 3, in .NET Core 5, you can specify the desired port under the applicationUrl for both IIS Express and your application profile in the launchSettings.json file.

  7. "ASP.NET Core 3 change default port in launchSettings.json without IIS Express"

    Code:

    {
      "profiles": {
        "YourAppName": {
          "commandName": "Project",
          "launchBrowser": true,
          "applicationUrl": "http://localhost:5001", // Change to desired port
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }
    

    Description: If you are not using IIS Express in ASP.NET Core 3, you can specify the desired port directly in the application profile of the launchSettings.json file.

  8. ".NET Core 5 change default port in launchSettings.json without IIS Express"

    Code:

    {
      "profiles": {
        "YourAppName": {
          "commandName": "Project",
          "launchBrowser": true,
          "applicationUrl": "http://localhost:5001", // Change to desired port
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        }
      }
    }
    

    Description: Similar to ASP.NET Core 3, if you are not using IIS Express in .NET Core 5, you can specify the desired port directly in the application profile of the launchSettings.json file.

  9. "Change default port in ASP.NET Core 3 with command-line arguments"

    Code:

    dotnet run --urls "http://localhost:5001"
    

    Description: When running your ASP.NET Core 3 application from the command line, you can specify the desired port using the --urls command-line argument.

  10. "Change default port in .NET Core 5 with command-line arguments"

    Code:

    dotnet run --urls "http://localhost:5001"
    

    Description: Similar to ASP.NET Core 3, when running your .NET Core 5 application from the command line, you can specify the desired port using the --urls command-line argument.


More Tags

self-signed extends font-size ibm-cloud payment ttk android-design-library setstate roblox backbone.js

More C# Questions

More Tax and Salary Calculators

More Organic chemistry Calculators

More Everyday Utility Calculators

More Chemical thermodynamics Calculators