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

  • Article

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:

  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

  • Azure API Management
  • Azure App Service
Publish an ASP.NET Core web API to Azure API Management with Visual Studio (2024)
Top Articles
What Are Ethereum Gas Fees? Ethereum’s Ether Transaction Fee | Gemini
Understand and manage Windows 10 virtual memory | TechTarget
Will Byers X Male Reader
1970 Chevrolet Chevelle SS - Skyway Classics
New Slayer Boss - The Araxyte
PRISMA Technik 7-10 Baden-Württemberg
Ventura Craigs List
DENVER Überwachungskamera IOC-221, IP, WLAN, außen | 580950
Hillside Funeral Home Washington Nc Obituaries
Caresha Please Discount Code
Sports Clips Plant City
Bjork & Zhulkie Funeral Home Obituaries
Calmspirits Clapper
How to find cash from balance sheet?
Hell's Kitchen Valley Center Photos Menu
Paradise leaked: An analysis of offshore data leaks
Fdny Business
Culver's Flavor Of The Day Taylor Dr
Xsensual Portland
Putin advierte que si se permite a Ucrania usar misiles de largo alcance, los países de la OTAN estarán en guerra con Rusia - BBC News Mundo
Toothio Login
Chamberlain College of Nursing | Tuition & Acceptance Rates 2024
Bay Area Craigslist Cars For Sale By Owner
Mta Bus Forums
Walgreens On Bingle And Long Point
Firefly Festival Logan Iowa
Evil Dead Rise Ending Explained
Myaci Benefits Albertsons
Darknet Opsec Bible 2022
Desales Field Hockey Schedule
Armor Crushing Weapon Crossword Clue
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Word Trip Level 359
Pokemmo Level Caps
Cruise Ships Archives
Samsung 9C8
Foolproof Module 6 Test Answers
Geology - Grand Canyon National Park (U.S. National Park Service)
Froedtert Billing Phone Number
Check From Po Box 1111 Charlotte Nc 28201
Frommer's Philadelphia & the Amish Country (2007) (Frommer's Complete) - PDF Free Download
Second Chance Apartments, 2nd Chance Apartments Locators for Bad Credit
888-822-3743
Tripadvisor Vancouver Restaurants
3 bis 4 Saison-Schlafsack - hier online kaufen bei Outwell
Academic Notice and Subject to Dismissal
Hillsborough County Florida Recorder Of Deeds
A jovem que batizou lei após ser sequestrada por 'amigo virtual'
Sapphire Pine Grove
Walmart Listings Near Me
Nurses May Be Entitled to Overtime Despite Yearly Salary
Colin Donnell Lpsg
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 6588

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.