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?
- Scalability: Easily scale your application up or down based on demand.
- Managed Infrastructure: Focus on your code, not server maintenance.
- Global Reach: Deploy your application to data centers worldwide.
- Integration: Seamlessly integrate with other Azure services like Azure SQL Database, Azure Cache for Redis, and Azure DevOps.
- Security: Benefit from Azure's comprehensive security features.
Step 1: Prepare Your .NET Application
Ensure your application is ready for deployment. This typically involves:
- .NET Version Compatibility: Verify that the .NET runtime version used by your application is supported by Azure App Service.
- Configuration: Configure your application for the production environment (e.g., connection strings, API keys). Using Azure App Settings is highly recommended.
- Build & Publish: Publish your application for the target environment (e.g., Release configuration).
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:
- Navigate to the Azure Portal.
- Click "+ Create a resource".
- Search for "Web App" and select it.
- Click "Create".
- Fill in the required details: Subscription, Resource Group, Name, Runtime stack (e.g., .NET 7), Region, and Operating System (Windows or Linux).
- 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.
- Zip Deploy: Upload a zipped version of your published application files.
- FTP: Use an FTP client to upload your files.
- Local Git: Configure a local Git repository to push your code directly.
Method 2: Visual Studio
If you're using Visual Studio, you can deploy directly:
- Right-click on your web project in Solution Explorer.
- Select "Publish...".
- Choose "Azure" as the target.
- Select "Azure App Service" and click "Next".
- Choose your subscription, resource group, and the App Service you created.
- 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.
- In your App Service, navigate to "Configuration".
- Under "Application settings", add key-value pairs for your configuration (e.g.,
ASPNETCORE_ENVIRONMENT=Production
, DatabaseConnectionString
).
- 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");