9.9 KiB
title | author | description | ms.author | ms.custom | ms.date | uid |
---|---|---|---|---|---|---|
Publish an ASP.NET Core web API to Azure API Management with Visual Studio | codemillmatt | Learn how to publish an ASP.NET Core web API to Azure API Management using Visual Studio. | masoucou | devx-track-csharp, mvc | 11/22/2020 | tutorials/publish-to-azure-api-management-using-vs |
Publish an ASP.NET Core web API to Azure API Management with Visual Studio
By Matt Soucoup
Set up
- Open a free Azure account if you don't have one.
- Create a new Azure API Management instance if you haven't already.
- Create a web API app project.
Configure the app
Adding Swagger definitions to the ASP.NET Core web API allows Azure API Management to read the app's API definitions. Complete the following steps.
Add Swagger
-
Add the Swashbuckle.AspNetCore NuGet package to the ASP.NET Core web API project:
-
Add the following line to the
Startup.ConfigureServices
method:services.AddSwaggerGen();
-
Add the following line to the
Startup.Configure
method:app.UseSwagger();
Change the API routing
Next, you'll change the URL structure needed to access the Get
action of the WeatherForecastController
. Complete the following steps:
-
Open the WeatherForecastController.cs file.
-
Delete the
[Route("[controller]")]
class-level attribute. The class definition will look like the following:[ApiController] public class WeatherForecastController : ControllerBase
-
Add a
[Route("/")]
attribute to theGet()
action. The function definition will look like the following:[HttpGet] [Route("/")] public IEnumerable<WeatherForecast> Get()
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.
- Add a blank API to the Azure API Management service instance.
- 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.
After creation is completed, the dialog is automatically closed and the Publish dialog gets focus again. The instance that was created is automatically selected.
At this point, you need to add an API to the Azure API Management service. Leave Visual Studio open while you complete the following tasks.
Add an API to Azure API Management
- Open the API Management Service instance created previously in the Azure portal. Select the APIs blade:
- Select the 3 dots next to Echo API and then select Delete from the pop-up menu to remove it.
- From the Add a new API panel, select the Blank API tile:
-
Enter the following values in the Create a blank API dialog that appears:
- Display Name: WeatherForecasts
- Name: weatherforecasts
- API Url suffix: v1
- Leave the Web service URL field empty.
-
Select the Create button.
The blank API is created.
Publish the ASP.NET Core web API to Azure API Management
-
Switch back to Visual Studio. The Publish dialog should still be open where you left off before.
-
Select the newly published Azure App Service so it's highlighted.
-
Select the Next button.
-
The dialog now shows the Azure API Management service created before. Expand it and the APIs folder to see the blank API you created.
-
Select the blank API's name and select the Finish button.
-
The dialog closes and a summary screen appears with information about the publish. Select the Publish button.
The web API will publish 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.
-
Switch back to the Azure API Management instance in the Azure portal.
-
Refresh the browser window.
-
Select the blank 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 different than what you named it. The published API is named WeatherAPI; however, you named it WeatherForecasts when you created it. Complete the following steps to fix the name:
-
Delete the following line from the
Startup.ConfigureServices
method:services.AddSwaggerGen();
-
Add the following code to the
Startup.ConfigureServices
method:services.AddSwaggerGen(config => { config.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "WeatherForecasts", Version = "v1" }); });
-
Open the newly created publish profile. It can be found from Solution Explorer in the Properties/PublishProfiles folder.
-
Change the
<OpenAPIDocumentName>
element's value fromv1
toWeatherForecasts
. -
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:
A successful response will look like the following:
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.