Deploying an ASP.NET Core application involves several steps, including preparing the application, choosing a hosting environment, and configuring the server. This guide will walk you through the essential steps to successfully deploy your ASP.NET Core application.

1. Prepare Your Application for Deployment

Before deploying your application, ensure that it is ready for production. This includes:

  • Testing the application thoroughly to catch any bugs.
  • Setting the environment to Production in the appsettings.json file or through environment variables.
  • Removing any development-only packages or configurations.

Sample Code for Setting Environment

        
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}

In this example, the appsettings.json file is configured for production logging levels.

2. Publish Your Application

Publishing your application compiles the code and prepares it for deployment. You can publish your application using the .NET CLI or Visual Studio.

Using the .NET CLI to Publish

        
dotnet publish -c Release -o ./publish

This command publishes the application in Release mode and outputs the files to the ./publish directory.

Using Visual Studio to Publish

In Visual Studio, right-click on the project in Solution Explorer and select Publish. Follow the prompts to configure the publishing settings and choose a target location.

3. Choose a Hosting Environment

ASP.NET Core applications can be hosted in various environments, including:

  • Windows Server with IIS: Ideal for traditional hosting environments.
  • Linux with Nginx or Apache: Suitable for cloud-based or containerized deployments.
  • Cloud Services: Platforms like Azure, AWS, or Google Cloud offer managed hosting solutions.

4. Deploy to a Windows Server with IIS

If you choose to deploy to a Windows Server using IIS, follow these steps:

Step 1: Install the .NET Core Hosting Bundle

Download and install the .NET Core Hosting Bundle on the server. This bundle includes the .NET Core Runtime, libraries, and the ASP.NET Core Module for IIS.

Step 2: Configure IIS

Open IIS Manager and create a new site:

  1. Right-click on Sites and select Add Website.
  2. Set the Site name, Physical path (pointing to the published folder), and Port.
  3. Click OK to create the site.

Step 3: Configure Application Pool

Ensure that the application pool is set to use No Managed Code since ASP.NET Core runs in a separate process.

Step 4: Set Permissions

Grant the IIS user (usually IIS_IUSRS) read and execute permissions to the published folder.

5. Deploy to Linux with Nginx

For deploying to a Linux server with Nginx, follow these steps:

Step 1: Install .NET Core Runtime

Install the .NET Core Runtime on your Linux server. You can follow the official documentation for installation instructions

specific to your Linux distribution.

Step 2: Configure Nginx

Create a new Nginx configuration file for your application:

        
server {
listen 80;
server_name your_domain.com;

location / {
proxy_pass http://localhost:5000; // Port where your app runs
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}

This configuration sets up Nginx to forward requests to your ASP.NET Core application running on port 5000.

Step 3: Start Your Application

Use the dotnet command to run your application:

        
dotnet yourapp.dll

Consider using a process manager like systemd or supervisor to keep your application running in the background.

6. Verify Your Deployment

After deploying your application, verify that it is running correctly by navigating to your domain in a web browser. Check the logs for any errors and ensure that the application behaves as expected.

Conclusion

Deploying an ASP.NET Core application involves several steps, including preparing the application, publishing it, choosing a hosting environment, and configuring the server. By following these steps, you can successfully deploy your ASP.NET Core application and make it accessible to users.