Redaguoti

Bendrinti naudojant


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.

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:

  1. Open the WeatherForecastController.cs file.

  2. 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:

  1. Publish the API app to Azure App Service.
  2. 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.

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

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

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

  5. Select the Create button.

  6. Once the app service is created, select the Next button.

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

  8. Select the Create button to proceed with the API Management service creation. This step may take several minutes to complete.

  9. When that completes, select the Finish button.

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

  11. Open up the Azure portal in a web browser and navigate to the API Management instance you created.

  12. Select the APIs option from the left-hand menu.

  13. 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:

  1. Add the following to Program.cs immediately after servies.AddSwaggerGen();

    builder.Services.ConfigureSwaggerGen(setup =>
    {
        setup.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
        {
            Title = "Weather Forecasts",
            Version = "v1"
        });
    });
    
  2. Republish the ASP.NET Core web API and open the Azure API Management instance in the Azure portal.

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

  1. Open the Test tab.
  2. Select / or the Get operation.
  3. Select Send.

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.

  2. In the Resource groups page, select Delete.

  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