Introduction
In the previous article, we learned how to Dockerize an ASP.NET Core Web API using Docker images and containers.
But running an API on our local machine is not enough when real users or other applications need to access it.
Today, we'll learn how to deploy an ASP.NET Core Web API to Microsoft Azure and make it available over the internet.
What Is Cloud Deployment?
Cloud deployment means hosting an application on cloud infrastructure instead of relying only on your local computer or physical server.
Once deployed, our Web API can be accessed by:
Web applications
Angular applications
Mobile applications
Other APIs
Users over the internet
For example, instead of running:
http://localhost:5000/api/products
our API may be hosted at a public URL such as:
https://my-product-api.azurewebsites.net/api/products
The exact URL is assigned by Azure when we create our hosting resource.
Why Deploy a Web API to Azure?
Azure provides cloud infrastructure and services that allow developers to host, manage, and scale applications.
Benefits
Public access to your API
Managed hosting infrastructure
HTTPS support
Application configuration
Monitoring and diagnostics
Deployment integration
Scaling options
Integration with other Azure services
Azure Services for Hosting an ASP.NET Core API
There are several ways to host an ASP.NET Core Web API on Azure.
1. Azure App Service
Azure App Service is a managed platform for hosting web applications and APIs.
It allows us to deploy our ASP.NET Core API without managing the underlying server operating system.
Best for:
Web APIs
Web applications
Small and medium applications
Production applications
Developers who want managed hosting
2. Azure Container Apps
Azure Container Apps allows us to deploy containerized applications without managing the underlying Kubernetes infrastructure directly.
Best for:
Containerized applications
Microservices
APIs
Event-driven workloads
Applications requiring container-based scaling
3. Azure Kubernetes Service (AKS)
Azure Kubernetes Service provides managed Kubernetes infrastructure for orchestrating containerized applications.
Best for:
Large container-based systems
Microservices architecture
Complex deployment requirements
Container orchestration at scale
For this tutorial, we'll use Azure App Service, because it is one of the simplest ways to deploy an ASP.NET Core Web API.
Azure Deployment Architecture
Step 1 – Prepare Your Web API
Before deployment, make sure your API is working correctly locally.
For example, your project might have the following structure:
ProductApi/
│
├── Controllers/
│ └── ProductsController.cs
├── Models/
├── Services/
├── appsettings.json
├── Program.cs
└── ProductApi.csproj
Run your application locally:
dotnet run
Test your API endpoints before deploying.
For example:
GET /api/products
GET /api/products/1
Resolve any errors before moving to Azure.
Step 2 – Create an Azure Account
To deploy applications, you need an Azure account.
Visit the official Azure website and sign in or create an account.
Azure may offer trial credits or free-tier services depending on your eligibility, region, and current offers. Always check pricing before creating resources.
Step 3 – Create an Azure App Service
We'll create a Web App resource to host our ASP.NET Core API.
Steps
Sign in to the Azure Portal.
-
Search for App Services.
-
Select Create.
-
Choose Web App.
Select your subscription and resource group.
Enter a unique application name.
Select the runtime stack that matches your application.
Choose a deployment region.
Select an appropriate pricing plan.
Review and create the resource.
Example Configuration
| Setting | Example |
|---|---|
| Resource | Web App |
| Name | my-product-api |
| Runtime | .NET |
| Operating System | Linux or Windows |
| Region | Select a suitable region |
| Plan | Choose based on your needs |
The available runtime versions, operating systems, and pricing plans can change, so select the options currently available in your Azure Portal.
Step 4 – Understand Azure App Service
Azure App Service provides a managed environment for hosting our application.
We don't need to manually install and maintain the web server and application runtime on our own virtual machine in the typical App Service deployment model.
Azure manages much of the hosting infrastructure.
Azure Cloud
|
┌──────┴───────┐
| App Service |
| |
| ASP.NET Core |
| Web API |
| |
└──────┬───────┘
|
Public HTTPS URL
Our application is deployed to Azure App Service and receives a public URL.
Step 5 – Publish Your ASP.NET Core API
Before deployment, we can publish our API.
Open the project directory in the terminal.
Run:
dotnet publish -c Release
The publish command prepares your application for deployment.
It creates a published output folder containing the application files and required dependencies.
Typically, the output is available under:
bin/Release/<target-framework>/publish/
For example, a .NET 10 project may publish to:
bin/Release/net10.0/publish/
Publishing is different from simply running the application using
dotnet run.
Step 6 – Deploy Using Visual Studio
Visual Studio provides a convenient deployment workflow.
Steps
Open your ASP.NET Core Web API project in Visual Studio.
Right-click the project.
-
Select Publish.
-
Choose Azure as the target.
-
Select Azure App Service.
Choose an existing App Service or create a new one.
Configure the deployment settings.
Review the configuration.
-
Click Publish.
Visual Studio builds and publishes your application to Azure.
Once deployment completes, Visual Studio may open the deployed website.
Step 7 – Deploy Using Azure CLI
You can also deploy using command-line tools.
Install Azure CLI if it isn't already installed.
Official documentation:
https://learn.microsoft.com/cli/azure/install-azure-cli
Sign in to Azure:
az login
List your subscriptions:
az account list
Select a subscription:
az account set --subscription "YOUR-SUBSCRIPTION-ID"
You can then use Azure CLI commands to create or manage your App Service resources.
For example, creating an App Service plan:
az appservice plan create --name my-api-plan --resource-group my-api-rg --sku B1 --is-linux
Creating a Web App:
az webapp create --resource-group my-api-rg --plan my-api-plan --name my-product-api --runtime "DOTNETCORE:10.0"
Azure CLI runtime identifiers and available App Service configurations can change. If the selected runtime is unavailable, check the current supported runtime options in Azure before running the command.
Step 8 – Deploy Published Files Using Azure CLI
For a straightforward deployment workflow, publish the API and deploy the published output.
First, publish your application:
dotnet publish -c Release -o ./publish
Then deploy the published folder to your App Service using the supported Azure deployment method.
Azure CLI provides deployment commands for App Service, and the exact command depends on the deployment mechanism and platform configuration.
For example, a ZIP deployment workflow:
Publish your application.
Package the published output.
Deploy the package to the App Service.
Verify that the application starts successfully.
The important point is to deploy the published application output, not your entire development project folder.
Step 9 – Configure Environment Settings
In development, applications may use local configuration files.
In production, environment-specific settings should be managed separately.
For example, your API may require:
Database connection strings
API keys
External service URLs
Environment-specific settings
Azure App Service allows us to configure application settings through its configuration interface.
In Azure Portal:
Open your App Service.
-
Navigate to Settings → Environment variables or the corresponding configuration area.
Add the required settings.
Save the configuration.
Restart the application if necessary.
Example:
ConnectionStrings__DefaultConnection
ASP.NET Core supports hierarchical configuration using double underscores
(__) in environment variable names.
For example:
ConnectionStrings__DefaultConnection
maps to:
{
"ConnectionStrings": {
"DefaultConnection": "..."
}
}
Important: Never commit production credentials, database passwords, or secrets to source control or place them directly into a Docker image.
Step 10 – Configure the Production Environment
ASP.NET Core applications use environments such as:
Development
Staging
Production
Azure App Service normally runs applications in the production environment unless configured otherwise.
You can check the environment in your application:
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
Development-specific tools and detailed diagnostics should not be exposed publicly in production unless intentionally secured and configured.
For example, Swagger/OpenAPI access should be configured according to your application's security requirements.
Step 11 – Test Your Deployed API
After deployment, Azure provides a public application URL.
For example:
https://my-product-api.azurewebsites.net
Suppose our API has the following endpoint:
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
[HttpGet]
public IActionResult GetProducts()
{
return Ok(new[]
{
new { Id = 1, Name = "Laptop" },
new { Id = 2, Name = "Mouse" }
});
}
}
Our endpoint becomes:
https://my-product-api.azurewebsites.net/api/products
Open the URL in a browser or use Postman to test it.
PowerShell
Invoke-RestMethod -Uri "https://my-product-api.azurewebsites.net/api/products" -Method Get
CMD
curl "https://my-product-api.azurewebsites.net/api/products"
If the API returns data successfully, your deployment is working.
Step 12 – Monitor Your Application
Deploying an API is not the end of the process.
We also need to monitor its health and performance.
Azure provides monitoring and diagnostics capabilities for App Service, including:
Application logs
HTTP request information
Performance metrics
Availability information
Diagnostic settings
Integration with Application Insights
Application Insights can help developers understand application behavior, failures, performance issues, and dependencies.
For production applications, monitoring should be part of the deployment strategy.
Step 13 – Understand the Deployment Workflow
Common Deployment Problems
1. API Works Locally but Not on Azure
Possible reasons:
Incorrect environment configuration
Missing dependencies
Incorrect connection strings
Application startup errors
Unsupported runtime configuration
Check the application logs.
2. Database Connection Failure
Your local database may not be accessible from Azure.
For example, if your connection string points to:
localhost
inside your local development environment, that doesn't mean Azure can access the same database.
You need a database hosted in an accessible environment and the correct connection configuration.
3. CORS Errors
If an Angular application hosted on a different domain calls your API, CORS must be configured correctly.
Remember, CORS is enforced by browsers and is separate from API authentication.
4. Application Starts but Endpoint Returns 404
Check:
Controller route
HTTP method
Application URL
API path
Deployment output
5. Secrets in Source Code
Never store production secrets directly in your source code.
Use Azure application settings or a suitable secrets-management solution.
Azure Deployment vs. Local Development
| Local Development | Azure Deployment |
|---|---|
| Runs on your computer | Runs on cloud infrastructure |
| Usually uses development settings | Uses production configuration |
| Local URLs | Public hosting URL |
| Local debugging | Cloud monitoring and diagnostics |
| Local database may be used | Cloud-accessible database configuration |
Key Takeaways
-
Cloud deployment makes our API accessible outside our local machine.
-
Azure App Service provides managed hosting for ASP.NET Core applications.
We can deploy using Visual Studio or command-line workflows.
-
dotnet publishprepares the application for deployment. -
Production configuration should be separated from development configuration.
Environment variables can override ASP.NET Core configuration.
Azure monitoring helps diagnose problems after deployment.
Always test the deployed API using its public URL.
Never expose secrets or sensitive configuration.
What's Next?
In Day 29, we'll build a Real-World ASP.NET Core Web API Project and bring together the concepts we've learned throughout this series, including architecture, dependency injection, authentication, data access, validation, logging, and API design.
Skills Tutorials – Learn • Build • Grow Together
Continue learning with our 30-day .NET Core Web API series.