Publish an ASP.NET Core web API to Azure API Management with Visual Studio

By Matt Soucoup

Set up

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

  1. Add the Swashbuckle.AspNetCore NuGet package to the ASP.NET Core web API project:

    Screenshot to configure the NuGet dialog

  2. Add the following line to the Startup.ConfigureServices method:

    services.AddSwaggerGen();
    
  3. 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:

  1. Open the WeatherForecastController.cs file.

  2. Delete the [Route("[controller]")] class-level attribute. The class definition will look like the following:

    [ApiController]
    public class WeatherForecastController : ControllerBase
    
  3. Add a [Route("/")] attribute to the Get() 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:

  1. Publish the API app to Azure App Service.
  2. Add a blank API to the Azure API Management service instance.
  3. 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:

  1. In Solution Explorer, right-click the project and select Publish:

    Contextual menu open with Publish link highlighted

  2. In the Publish dialog, select Azure and select the Next button:

    Publish dialog

  3. Select Azure App Service (Windows) and select the Next button:

    Publish dialog: select App Service

  4. Select Create a new Azure App Service.

    Publish dialog: select Azure Service instance

    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.

  5. Select the Create button.

    Create App Service dialog

After creation is completed, the dialog is automatically closed and the Publish dialog gets focus again. The instance that was created is automatically selected.

Publish dialog: select App Service instance

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

  1. Open the API Management Service instance created previously in the Azure portal. Select the APIs blade:

APIs blade selected from the API Management Service instance

  1. Select the 3 dots next to Echo API and then select Delete from the pop-up menu to remove it.

Deleting the echo API from the API Management Service instance

  1. From the Add a new API panel, select the Blank API tile:

Screen showing the blank API tile highlighted

  1. 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.
  2. Select the Create button.

    Screenshot of the completed create a blank api dialog

The blank API is created.

Screenshot of a blank API in the API Management portal

Publish the ASP.NET Core web API to Azure API Management

  1. Switch back to Visual Studio. The Publish dialog should still be open where you left off before.

  2. Select the newly published Azure App Service so it's highlighted.

  3. Select the Next button.

    Screenshot of the publish dialog with the app service selected

  4. The dialog now shows the Azure API Management service created before. Expand it and the APIs folder to see the blank API you created.

  5. Select the blank API's name and select the Finish button.

    Screenshot of the newly created Azure API Management blank API selected in the publish dialog

  6. The dialog closes and a summary screen appears with information about the publish. Select the Publish button.

    Screenshot of Visual Studio with the publish profile displayed

    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.

  7. Switch back to the Azure API Management instance in the Azure portal.

  8. Refresh the browser window.

  9. Select the blank API you created in the preceding steps. It's now populated and you can explore around.

    Screenshot of the populated API in Azure API Management

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:

Screenshot of populated API with different names highlighted

  1. Delete the following line from the Startup.ConfigureServices method:

    services.AddSwaggerGen();
    
  2. Add the following code to the Startup.ConfigureServices method:

    services.AddSwaggerGen(config =>
    {
        config.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
        {
            Title = "WeatherForecasts",
            Version = "v1"
        });
    });
    
  3. Open the newly created publish profile. It can be found from Solution Explorer in the Properties/PublishProfiles folder.

    Screenshot showing the publish profile file location highlighted

  4. Change the <OpenAPIDocumentName> element's value from v1 to WeatherForecasts.

    screenshot of the changes necessary for the publish profile

  5. Republish the ASP.NET Core web API and open the Azure API Management instance in the Azure portal.

  6. Refresh the page in your browser. You'll see the name of the API is now correct.

    Screenshot of the finished API in the portal

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:

  1. Open the Test tab.

  2. Select / or the Get operation.

  3. Select Send.

    Screenshot of the portal before the test

A successful response will look like the following:

Screenshot of a successful response from API Management

Clean up

When you've finished testing the app, go to the Azure portal and delete the app.

  1. Select Resource groups, then select the resource group you created.

    Azure Portal: Resource Groups in sidebar menu

  2. In the Resource groups page, select Delete.

    Azure Portal: Resource Groups page

  3. 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.

Additional resources