How to specify the port an asp net core application is hosted on

Introduction

When developing an ASP.NET Core application, you may need to specify the port on which the application is . This can be useful in various scenarios, such as when you want to run multiple on different ports or when you want to test your application on a specific port. In this article, we will explore different ways to specify the port for an ASP.NET Core application.

Using the appsettings.json file

One way to specify the port for an ASP.NET Core application is by using the appsettings.json file. This file is commonly used to store configuration settings for the application. To specify the port, open the appsettings.json file and add the following code:


"Kestrel": {
  "EndPoints": {
    "Http": {
      "Url": "http://localhost:5001"
    }
  }
}

In this example, we have that the application should be hosted on port 5001. You can change the port number to any desired value. Make sure to save the appsettings.json file after making the changes.

Using -line arguments

Another way to specify the port for an ASP.NET Core application is by using command-line arguments. This can be useful when you want to quickly change the port modifying the appsettings.json file. To specify the port using command-line arguments, open a command or terminal and navigate to the project . Then, run the following command:


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

In this example, we have specified that the application should be hosted on port 5001. You can change the port number to any desired value. After running the command, the application will start and be accessible on the specified port.

Using environment

Another way to specify the port for an ASP.NET Core application is by using environment variables. This can be useful when you want to deploy your application to different environments with different port . To specify the port using environment variables, open the launchSettings.json file located in the Properties folder of your project. Then, add the following code:


"environmentVariables": {
  "ASPNETCORE_URLS": "http://localhost:5001"
}

In this example, we have specified that the application should be hosted on port 5001. You can change the port number to any desired value. After making the changes, save the launchSettings.json file.

Conclusion

In this article, we have explored different ways to specify the port for an ASP.NET Core application. Whether it is through the appsettings.json file, command-line arguments, or environment variables, you have the flexibility to choose the method that best suits your needs. By the port, you can have better control over how your application is hosted and accessed.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents