Publish an ASP.NET Core web API to Azure API Management with Visual Studio
By Matt Soucoup
In this tutorial you'll learn how to create an ASP.NET Core web API project using Visual Studio, ensure it has OpenAPI support, and then publish the web API to both Azure App Service and Azure API Management.
Set up
To complete the tutorial you'll need an Azure account.
- Open a free Azure account if you don't have one.
Create an ASP.NET Core web API
Visual Studio allows you to easily create a new ASP.NET Core web API project from a template. Follow these directions to create a new ASP.NET Core web API project:
- From the File menu, select New > Project.
- Enter Web API in the search box.
- Select the ASP.NET Core Web API template and select Next.
- In the Configure your new project dialog, name the project WeatherAPI and select Next.
- In the Additional information dialog:
- Confirm the Framework is .NET 6.0 (Long-term support).
- Confirm the checkbox for Use controllers (uncheck to use minimal APIs) is checked.
- Confirm the checkbox for Enable OpenAPI support is checked.
- Select Create.
Explore the code
Swagger definitions allow Azure API Management to read the app's API definitions. By checking the Enable OpenAPI support checkbox during app creation, Visual Studio automatically adds the code to create the Swagger definitions. Open up the Program.cs
file which shows the following code:
...
builder.Services.AddSwaggerGen();
...
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(); // Protected by if (env.IsDevelopment())
}
...
Ensure the Swagger definitions are always generated
Azure API Management needs the Swagger definitions to always be present, regardless of the application's environment. To ensure they are always generated, move app.UseSwagger();
outside of the if (app.Environment.IsDevelopment())
block.
The updated code:
...
app.UseSwagger();
if (app.Environment.IsDevelopment())
{
app.UseSwaggerUI();
}
...
Change the API routing
Change the URL structure needed to access the Get
action of the WeatherForecastController
. Complete the following steps:
Open the
WeatherForecastController.cs
file.Replace the
[Route("[controller]")]
class-level attribute with[Route("/")]
. The updated class definition :[ApiController] [Route("/")] public class WeatherForecastController : ControllerBase
Publish the web API to Azure App Service
Complete the following steps to publish the ASP.NET Core web API to Azure API Management:
- Publish the API app to Azure App Service.
- Publish the ASP.NET Core web API app to the Azure API Management service instance.
Publish the API app to Azure App Service
Complete the following steps to publish the ASP.NET Core web API to Azure API Management:
In Solution Explorer, right-click the project and select Publish.
In the Publish dialog, select Azure and select the Next button.
Select Azure App Service (Windows) and select the Next button.
Select Create a new Azure App Service.
The Create App Service dialog appears. The App Name, Resource Group, and App Service Plan entry fields are populated. You can keep these names or change them.
Select the Create button.
Once the app service is created, select the Next button.
Select Create a new API Management Service.
The Create API Management Service dialog appears. You can leave the API Name, Subscription Name, and Resource Group entry fields as they are. Select the new button next to the API Management Service entry and enter the required fields from that dialog box.
Select the OK button to create the API Management service.
Select the Create button to proceed with the API Management service creation. This step may take several minutes to complete.
When that completes, select the Finish button.
The dialog closes and a summary screen appears with information about the publish. Select the Publish button.
The web API publishes to both Azure App Service and Azure API Management. A new browser window will appear and show the API running in Azure App Service. You can close that window.
Open up the Azure portal in a web browser and navigate to the API Management instance you created.
Select the APIs option from the left-hand menu.
Select the API you created in the preceding steps. It's now populated and you can explore around.
Configure the published API name
Notice the name of the API is named WeatherAPI; however, we would like to call it Weather Forecasts. Complete the following steps to update the name:
Add the following to
Program.cs
immediately afterservies.AddSwaggerGen();
builder.Services.ConfigureSwaggerGen(setup => { setup.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "Weather Forecasts", Version = "v1" }); });
Republish the ASP.NET Core web API and open the Azure API Management instance in the Azure portal.
Refresh the page in your browser. You'll see the name of the API is now correct.
Verify the web API is working
You can test the deployed ASP.NET Core web API in Azure API Management from the Azure portal with the following steps:
- Open the Test tab.
- Select / or the Get operation.
- Select Send.
Clean up
When you've finished testing the app, go to the Azure portal and delete the app.
Select Resource groups, then select the resource group you created.
In the Resource groups page, select Delete.
Enter the name of the resource group and select Delete. Your app and all other resources created in this tutorial are now deleted from Azure.