MSDN Community Topics

Your source for in-depth developer guidance and support.

Deploying .NET Web Applications to Azure

Last updated: October 26, 2023 | By: Azure Community Experts

Azure provides a robust and scalable platform for hosting your .NET web applications. This guide walks you through the essential steps and best practices for deploying your ASP.NET Core, ASP.NET MVC, or Blazor applications to Azure App Service.

Why Deploy to Azure?

Step 1: Prepare Your .NET Application

Ensure your application is ready for deployment. This typically involves:

For ASP.NET Core, you'll typically publish to a folder:

dotnet publish -c Release -o ./publish

Step 2: Create an Azure App Service

You can create an Azure App Service through the Azure portal, Azure CLI, or Visual Studio.

Using the Azure Portal:

  1. Navigate to the Azure Portal.
  2. Click "+ Create a resource".
  3. Search for "Web App" and select it.
  4. Click "Create".
  5. Fill in the required details: Subscription, Resource Group, Name, Runtime stack (e.g., .NET 7), Region, and Operating System (Windows or Linux).
  6. Choose a plan (Free, Shared, Basic, Standard, Premium) that suits your needs.

Using Azure CLI:


Create a resource group (if needed):
az group create --name MyResourceGroup --location "East US"

Create an App Service Plan:
az appservice plan create --name MyAppServicePlan --resource-group MyResourceGroup --sku B1 --is-linux

Create the Web App:
az webapp create --resource-group MyResourceGroup --plan MyAppServicePlan --name MyDotnetWebApp --runtime "DOTNETCORE:7.0"
            

Step 3: Deploy Your Application

Several methods are available for deploying your code:

Method 1: Deployment Center (Zip Deploy / FTP / Local Git)

Navigate to your App Service in the Azure portal, go to "Deployment Center", and choose your preferred deployment method.

Method 2: Visual Studio

If you're using Visual Studio, you can deploy directly:

  1. Right-click on your web project in Solution Explorer.
  2. Select "Publish...".
  3. Choose "Azure" as the target.
  4. Select "Azure App Service" and click "Next".
  5. Choose your subscription, resource group, and the App Service you created.
  6. Click "Finish" and then "Publish".

Method 3: Azure CLI


Navigate to your publish directory and deploy:
cd publish
az webapp deploy --resource-group MyResourceGroup --name MyDotnetWebApp --src-path . --type zip
            

Method 4: CI/CD with Azure DevOps or GitHub Actions

For automated deployments, integrate with CI/CD pipelines. This is the recommended approach for production environments.

Step 4: Configure Application Settings and Connection Strings

Manage your application's configuration securely in Azure App Service.

  1. In your App Service, navigate to "Configuration".
  2. Under "Application settings", add key-value pairs for your configuration (e.g., ASPNETCORE_ENVIRONMENT=Production, DatabaseConnectionString).
  3. For database connection strings, use the "Connection strings" tab for better management.

Your .NET application can access these settings via environment variables or the IConfiguration service.


// Accessing configuration in ASP.NET Core
var connectionString = _configuration["DatabaseConnectionString"];
var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");