A beginner guide how to deploy a ASP .NET Core Web API to Azure
In this article I will show you how to deploy a ASP .NET Core Web API to Azure. Even if this a beginner guide you need to have some basic knowledge of Azure Web Apps, Visual Studio Code and ASP .NET Core Web API.
What you need for development enviroment
Tools you need to get started are:
- Visual Studio Code Download VS Code
- Free Microsoft Azure account Azure Free Account
- .NET Core 2.2
- Postman Download Postman
Well let’s get started!
1. Create ASP .NET Core Web API
To create a template ASP .NET Core Web API I’ll use .NET Core CLI and VS Code to inspect the code but you can use Visual Studio 2017 or 2019. The result will be the same.
Open command prompt, I use
git bash and enter the command:
dotnet new webapi --name AzAPI
and a project with just the basic for a API it is created in my desktop.
Then enter the
cd AzPI command to get inside the project folder and execute the Web API with the
donet run command.
In command prompt you will see the message
Now listening on: https://localhost:5001 copy the link and
/api/values at the end of the link. Now you can check the browser your API executed.
Using Visual Studio Code to work with the code
First stop the project running with
Ctrl+C and enter the command
code . to open the project in VS Code.
As you see in Explorer on VS Code the project has different files and folder.
Startup.cs – .NET Core use this class to configure services needed by the application and to define the request handling pipeline. The class include two methods:
- ConfigureServices – wich is used for service configuration to the container and consumed across the app via DI (dependency injection). It is optional and called by the web server before the Configure method.
- Configure – the method it is required and create the app’s request processing pipeline.
Program.cs – It is the starting point of the app and used to create a web server.
Working with Postman
Because we are working with RESTful API we need to test our rest methods in Postman.
Postman is a powerful HTTP client for testing web services.
Postman makes it easy to test, develop and document APIs by allowing users to quickly put together both simple and complex HTTP requests. Postman is available as both a Google Chrome Packaged App and a Google Chrome in-browser app.
To test the pre-existing API Controller
ValuesController which route is
api/values first we need to execute the app with
dotnet run and second enter the full url `http://localhost:5000/api/values in Postman.
In the photo above I have tested the method GET but you can use Postman to test others methods that your app contains.
Deploy the .Web API to Azure
To deploy the web application to Azure, we need an Azure account. If you don’t have one, you may create a free account in Azure at first.
Once we have the Azure account ready, we need to create a new using VS Code.
Before start creating the web app in Azure we need to install the Azure App Service extension in VS Code.
Azure App Service on VS Code search bar.
Well now let’s start deploying our web app. There are two ways for you to create the web app.
- Click the Azure App Service extension. Log into your Azure account - in the Activity Bar, click on the Azure logo to show the AZURE APP SERVICE explorer. Click Sign in to Azure… and follow the instructions.
Ctrl+Shift+pto display the command palette in VS Code and select
Create new web app
After the is created you’ll prompted to
Deploy to web app?.
Press Yes and then in command palette you need to select the path of the folder where your project is.
After few seconds you’ll see a prompt in right-down corner with the message
Deployment to az-api completed.
Click Browse WebSite and enter
api/values at the end of url and finaly you’ll see the app diplaying on the browser the same json message as above.