In this article we are going to learn step by step how to deploy .Net API to azure app service. We'll create an App Service on Azure, publish the API from Visual Studio, set up connection strings, and make sure the app is actually running after deployment. No skipping steps.
This tutorial shows how to:
- Create an Azure App Service from the portal
- Publish a .NET API from Visual Studio directly to Azure
- Configure environment variables and connection strings in Azure
- Enable logging to debug issues after deployment
- Test the live API endpoint
Why Azure App Service for .NET API
You could go with Azure Container Apps, Azure Functions, or even a plain VM. But App Service is the easiest starting point for a .NET API. It handles the server management, OS updates, scaling, and SSL certificate for your domain — all without you touching any of that.
For most .NET APIs in early or mid stage, App Service is more than enough. You pay for what you use, you can scale up with one click, and deployment from Visual Studio is built in. That last part especially saves a lot of time.
What You Need Before Starting
Make sure you have these ready before jumping in:
- An Azure account. Free tier works fine for testing — go to portal.azure.com and sign up if you haven't.
- Visual Studio 2022 with the Azure development workload installed
- A .NET 6, 7, or 8 Web API project ready to deploy
If your project is on .NET Framework (not .NET Core), some steps will be slightly different. This guide assumes .NET 6 or above.
Step 1 : Create an App Service on Azure Portal
Go to portal.azure.com and log in.
Click Create a resource from the home screen. Search for App Service and click on it. Then click Create.
You'll see a form with several fields. Fill them like this:
- Subscription — pick your subscription
- Resource Group — create a new one, give it a name like
my-api-rg - Name — this becomes part of your URL. Something like
my-dotnet-api. Has to be unique across Azure. - Publish — select Code
- Runtime stack — pick your .NET version. .NET 8 if that's what you're using.
- Operating System — Windows works smoothly with .NET APIs. Pick Windows.
- Region — pick the one closest to your users. Central India or South India if your users are in India.
Scroll down to App Service Plan. This controls the pricing tier. For testing, Free F1 is fine. For production, go with at least B1 Basic.
Click Review + Create, then Create. Azure takes about 2 minutes to provision everything.
Once done, click Go to resource. You'll land on your App Service dashboard. Keep this tab open.
Step 2 : Publish from Visual Studio
Open your .NET API project in Visual Studio 2022.
Right-click on the project in Solution Explorer. Click Publish.
A dialog opens. Select Azure as the target and click Next.
Then select Azure App Service (Windows) and click Next.
Now it will ask you to sign in to your Azure account if you haven't already. Sign in and your subscriptions will load. Find the App Service you just created in the list and select it.
Click Finish. Visual Studio creates a publish profile for this App Service.
Before hitting Publish, click Show all settings on the publish profile page. Check the Configuration — make sure it says Release, not Debug. You don't want to deploy a debug build to production.
Now click Publish. Visual Studio builds the project and uploads it to Azure. Takes a minute or two depending on project size.
When it's done, Visual Studio opens the live URL in your browser automatically. Something like https://my-dotnet-api.azurewebsites.net.
If you see the default Azure page or a 503 error, don't panic. The app might still be starting up. Wait 30 seconds and refresh.
Step 3 : Set Up Connection Strings and Environment Variables
Your API probably talks to a database or reads config values from appsettings.json. In production you never hardcode these. Azure App Service has a place for this — Configuration settings.
Go to your App Service in the Azure portal. In the left menu, click Configuration.
You'll see two tabs — Application settings and Connection strings.
Application settings — these become environment variables in your app. Anything you read with Configuration["KeyName"] or Environment.GetEnvironmentVariable("KeyName") in .NET comes from here.
Click New application setting and add your values:
Name : ASPNETCORE_ENVIRONMENT
Value : Production
Add any other config values your app needs — API keys, feature flags, external service URLs, etc.
Connection strings — for database connections specifically. Click New connection string and add:
Name : DefaultConnection
Value : Server=your-server.database.windows.net;Database=yourdb;User Id=youruser;Password=yourpassword;
Type : SQLServer
In your .NET app, you read this the same way as before:
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
Azure injects these values at runtime. They override whatever is in appsettings.json. So your local appsettings.json can have local DB settings and Azure will use the production values automatically.
Click Save at the top. Azure will restart your app after saving — that's normal.
Step 4 : Enable Logging to Debug Issues
App deployed but something's not working right? First thing to check is the logs.
In the left menu of your App Service, click App Service logs.
Turn on Application logging (Filesystem). Set Level to Information or Error depending on how much detail you want. Click Save.
Now go back to the left menu and click Log stream. This shows live logs from your running app. Make a request to your API and watch the logs come in real time.
If your app is crashing on startup, the error will show here. Most of the time it's a missing environment variable, wrong connection string, or a dependency that isn't registered in Program.cs.
Another useful place — Diagnose and solve problems in the left menu. Azure has built-in diagnostics that can detect common issues like high CPU, memory leaks, or failed requests automatically.
Step 5 : Test the Live API
Once the app is running, test your endpoints using the live URL.
If you have Swagger enabled in your API, just go to:
https://my-dotnet-api.azurewebsites.net/swagger
Swagger UI should load and you can test all your endpoints from there.
To keep Swagger available in production, make sure your Program.cs doesn't restrict it to Development only:
// Remove the if condition or adjust it
app.UseSwagger();
app.UseSwaggerUI();
Or if you want Swagger only in non-production, check the environment:
if (app.Environment.IsDevelopment() || app.Environment.IsStaging())
{
app.UseSwagger();
app.UseSwaggerUI();
}
For testing specific endpoints without Swagger, use Postman. Just replace localhost with your Azure URL. Everything else stays the same — headers, body, auth tokens.
Step 6 : Set Up Custom Domain and SSL (Optional but Recommended)
By default your API runs on azurewebsites.net. For production you'll want your own domain like api.yourcompany.com.
Go to your App Service and click Custom domains in the left menu.
Click Add custom domain. Enter your domain name. Azure will show you a TXT record and a CNAME record to add in your DNS provider (GoDaddy, Namecheap, Cloudflare — wherever your domain is managed).
Add those records in your DNS provider. Come back to Azure and click Validate. Once DNS propagates (can take a few minutes to a few hours), click Add custom domain.
For SSL — click TLS/SSL settings in the left menu. Azure App Service can generate a free managed certificate for your custom domain. Click Private Key Certificates then Create App Service Managed Certificate. Select your domain and create it.
Then go back to Custom domains, click the binding next to your domain and select the certificate you just created. Done. Your API now runs on HTTPS with your own domain.
Common Issues After Deployment
502 Bad Gateway — App crashed on startup. Check Log stream immediately. Usually a missing config value or connection string.
The app works locally but not on Azure — 99% of the time it's an environment variable or connection string that's missing in Azure Configuration settings.
Database connection fails — If you're using Azure SQL, make sure the App Service's outbound IP addresses are added to the SQL server firewall. Go to your SQL Server in Azure portal → Networking → add the IPs shown in your App Service's Properties page.
Slow cold starts — Free and Basic tier App Services sleep after inactivity. First request after sleep takes 10-30 seconds. Upgrade to Standard tier or enable Always On in Configuration → General settings.
Summary
You learned how to deploy a .NET API to Azure App Service. You covered :
- Creating an App Service on Azure portal with the right settings
- Publishing directly from Visual Studio 2022 using the built-in publish flow
- Setting up Application settings and Connection strings in Azure Configuration
- Enabling Application logging and using Log stream for debugging
- Testing live endpoints with Swagger and Postman
- Setting up a custom domain and free SSL certificate
- Common post-deployment issues and how to fix them
Azure App Service takes a bit of getting used to but once you've done it once, the second deploy takes maybe 5 minutes. Visual Studio handles most of the heavy lifting and Azure manages the server for you.
I hope you like this article...
Happy coding! 🚀
0 Comments