
Setting up an Azure Web API is a straightforward process that can be completed in a few steps. You'll need to create a new Azure Web API project using Visual Studio, which can be done by selecting the "ASP.NET Web Application" template and choosing the "Web API" option.
To host your Azure Web API, you'll need to create a new Azure App Service, which can be done through the Azure portal. This will give you a scalable and secure hosting environment for your API.
Once you've created your Azure Web API project and App Service, you'll need to deploy your code to the App Service. This can be done using Azure DevOps or by manually uploading your code to the App Service.
For more insights, see: Azure Code
Setting Up Azure Web API
To set up your Azure Web API, you'll first need to create an App Registration in Azure AD for the Web API. This can be done by searching for "App Registrations" in the Azure Portal and creating a new App Registration, giving it a name like "My Web API".
The "Supported account type" should be left set to the default of "Accounts in this organizational directory only", and then click "Register". This will generate an Application ID URI that looks something like "api://50bad0fb-43f8-4599-a38e-faf49e40c7d0", which is also your "Application (client) ID".
You'll need to copy the secret value and store it somewhere safe after generating it, as you won't be able to access it again afterwards.
Related reading: Hacking Apis Breaking Web Application Programming Interfaces
Create ASP.NET Core
To create an ASP.NET Core web API, you can use Visual Studio to easily create a new project from a template. Select New > Project from the File menu, and then enter "Web API" in the search box.
The ASP.NET Core Web API template is available, so select it and then Next. In the Configure your new project dialog, name your project, such as "WeatherAPI", and select Next.
Confirm that the Framework is set to .NET 6.0 (Long-term support) and that the checkbox for Use controllers is checked. Also, confirm that the checkbox for Enable OpenAPI support is checked.
A fresh viewpoint: Azure Core
To publish the API to Azure API Management, follow these steps:
- Complete the project creation process.
- Go to the Publish section of the project properties.
- Choose Azure API Management as the publish target.
- Enter your Azure API Management settings.
By following these steps, you can create a new ASP.NET Core web API project and set it up for publishing to Azure API Management.
Create Service Instance
To create a service instance in Azure, you'll need to select Azure and click the Next button. This will open up a new window where you can create a new instance of App Service in Azure.
Provide a name for the App Service Instance, and select your Azure Subscription and Resource Group from the respective dropdown lists. You can also create a new Hosting Plan by clicking on the New link next to the Hosting Plan dropdown list.
A new window will appear, allowing you to create a new Hosting Plan. Provide a name for the hosting plan, select a Location, and choose the size. Click OK to save your changes.
You should now be back on the App Service window, where you can click Create to create the App Service instance in Azure. After a few seconds, the instance will be created, and it will be selected by default.
Additional reading: Azure Web Hosting Plans
Configuring API
You'll also need to republish the ASP.NET Core web API and refresh the page in your browser to see the updated API name.
CORS policy can be configured by navigating to the CORS menu under the API section of the Azure API Apps. Simply add the URL of the Client App to the list of allowed origins. This will allow the client app to access the API and fetch data.
To configure CORS, follow these steps:
- Navigate to the CORS menu under the API section of the Azure API Apps.
- Add the URL of the Client App to the list of allowed origins.
This will enable the client app to access the API and fetch data.
Configure Published Name
You can change the name of your published API to match your project's title.
To update the name, start by adding the following code to Program.cs immediately after services.AddSwaggerGen():
builder.Services.ConfigureSwaggerGen(setup =>
{
setup.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
{
Title = "Weather Forecasts",
Version = "v1"
});
});
This code sets the title of the API to "Weather Forecasts" and the version to "v1".
Next, republish the ASP.NET Core web API and open the Azure API Management instance in the Azure portal.
Refreshing the page in your browser will show the updated name of the API.
The name of the API is now correct, and you can see the changes you've made.
Additional reading: Azure Devops Version Control
Building ASP.NET Core with Swagger
Building ASP.NET Core with Swagger requires some configuration. To start, you'll need to install the Swashbuckle.AspNetCore NuGet package.
You can add this package using the dotnet add package Swashbuckle.AspNetCore command. This will enable Swagger support for your API.
To configure Swagger, you'll need to make changes in the ConfigureServices() method of the Startup.cs class. First, you'll need to add the Swagger middleware to the services collection.
The updated code should look something like this: services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }); });
Finally, you'll need to enable the Swagger middleware in the Configure() method. This can be done by adding the following code: app.UseSwagger(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API v1"));
Here's a summary of the steps to enable Swagger support for your ASP.NET Core Web API:
By following these steps, you'll be able to add Swagger support to your ASP.NET Core Web API and make it easier to test and document your API.
Create a Registration
In the Azure Portal, search for "App Registrations" and create a new App Registration. Give it a name like "My Web API".
Leave "Supported account type" set to the default of "Accounts in this organizational directory only", and click "Register". This will create the first App Registration for the Web API.
To edit the manifest, select "Manifest" in the App Registration left-hand menu. Find the place in the JSON that defines the appRoles and paste in a new DaemonAppRole.
This will generate an Application ID URI that looks something like this: api://50bad0fb-43f8-4599-a38e-faf49e40c7d0. You can also find the GUID in the Overview tab of the portal, which is your "Application (client) ID".
Next, add a secret in the "Certificates & secrets" section by choosing "new client secret" and an appropriate expiry time. Be sure to copy the secret value and store it somewhere safe.
For your interest: Web Application Programming Interface
Publishing and Deploying
Publishing and Deploying your Azure Web API is a straightforward process. You can publish your API app to Azure App Service by right-clicking the project and selecting Publish.
In the Publish dialog, select Azure and proceed to create a new Azure App Service. You can keep the default names or change them to your preference. Once created, you can create a new API Management Service, which may take several minutes to complete.
To deploy your API, you can connect your favorite version control system and automatically deploy commits. This makes code changes easier than ever. You can also use deployment slots to move your API to production, run tests, and redirect traffic to the new version without downtime.
Here are the steps to publish your API app to Azure App Service:
- Right-click the project and select Publish.
- Select Azure and proceed to create a new Azure App Service.
- Create a new API Management Service.
- Connect your favorite version control system and automatically deploy commits.
- Use deployment slots to move your API to production, run tests, and redirect traffic to the new version without downtime.
Publish to Service
Once you've built and tested your application, it's time to publish it to a service. You can use a service like Azure App Service to host your application.
Azure App Service supports a variety of programming languages, including C#, Java, and Python. This means you can use the language you're most comfortable with.
See what others are reading: How to Access Azure Key Vault in C#
To publish your application, you'll need to create a new deployment slot in Azure App Service. This allows you to test your application in a production-like environment before making it live.
Deployment slots can be configured to automatically swap with the production slot when you're ready to go live. This makes it easy to test and deploy your application without downtime.
You can also use Azure DevOps to automate the deployment of your application. This involves creating a pipeline that builds and deploys your application to Azure App Service.
Readers also liked: Azure Dev Test Subscription
Publish to App Service
Publishing to App Service is a straightforward process. You can start by right-clicking the project in Solution Explorer and selecting Publish.
To publish to Azure App Service, select Azure and click the Next button. You'll then choose Azure App Service (Windows) and click Next again. The Create App Service dialog will appear, where you can populate the App Name, Resource Group, and App Service Plan entry fields.
You can keep these names or change them as you see fit. Once you're satisfied, click the Create button to proceed. This step may take a few minutes to complete.
Here's a summary of the steps:
Right-click the project and select Publish.Select Azure and click the Next button.Choose Azure App Service (Windows) and click Next again.Populate the Create App Service dialog with your desired settings.Click the Create button to proceed.
After creating the app service, you'll need to create a new API Management Service. This involves selecting Create a new API Management Service and populating the required fields. Once you've completed this step, click the OK button to create the API Management service.
CI/CD Slots
CI/CD slots are a game-changer for deploying your API without downtime.
You can automatically deploy commits from your version control system, making code changes a breeze. This means you can make updates to your API without manually uploading new code, saving you time and effort.
By using deployment slots, you can run tests against a copy of your app, ensuring that everything works as expected before redirecting traffic to the new version. This helps catch any issues before they affect your live app.
Broaden your view: Azure Api Version
Security and Authentication
To secure your Azure Web API, you need to grant the Azure CLI permission to access the Web API app registration. This involves adding a new scope in the "Expose an API" tab of the App Registration.
You'll need to select "Add a client application" and use the client ID 04b07795-8ddb-461a-bbee-02f9e1bf7b46, which is the client ID of the Azure CLI. Then, choose the scope you created as an authorized scope.
To get an access token for the Web API, you'll need to provide your tenant ID and the scope for the Web API. You can do this in PowerShell by using the az account get-access-token command.
You can inspect the access token in jwt.io to see the information it includes. This will give you a better understanding of what's being passed to your Web API.
Remember to update the access token with your own tenant ID and Web API client ID. This will ensure that your requests are authenticated correctly.
See what others are reading: Azure Powershell vs Cli
Routing and Connectivity
Routing and Connectivity is a crucial aspect of Azure Web API. To change the API routing, you need to update the URL structure.
To access the Get action of the WeatherForecastController, you can replace the [Route("[controller]")] class-level attribute with [Route("/")]. This will update the class definition to [ApiController]
[Route("/")]
public class WeatherForecastController : ControllerBase.
This change will affect how the API is accessed, so make sure to test it thoroughly to ensure everything is working as expected.
Expand your knowledge: Azure Functions Startup Class
Configuring CORS
Configuring CORS is a crucial step in ensuring seamless communication between your client and API apps. To do this, you can navigate to the CORS menu under the API section of the Azure API Apps.
Once you're there, simply add the URL of your client app to the list of allowed origins. This will grant the client app permission to access the API and fetch data.
By making this change, you'll have successfully configured the CORS policy for your Azure API App. This will enable your client app to communicate with the API without any issues.
For another approach, see: Azure Remote Desktop Web Client
Change the Routing

Changing the routing of your API can be a bit tricky, but don't worry, it's easier than you think.
To change the API routing, you'll need to update the URL structure. This can be done by modifying the class-level attribute in the controller file.
Replacing the [Route("[controller]")] attribute with [Route("/")] is a simple step that can have a big impact. This updated attribute is used in the WeatherForecastController.cs file.
Here's a step-by-step guide to changing the routing:
- Open the WeatherForecastController.cs file.
- Replace the [Route("[controller]")] attribute with [Route("/")] in the class definition.
The updated class definition should look like this:
[ApiController]
[Route("/")]
public class WeatherForecastController : ControllerBase
On Premises Connectivity
Connecting your API to your corporate or local network is a great way to ensure enterprise-grade security.
You can use on-premises connections to serve APIs to your intranet as if they were running locally.
This means you can access your APIs from anywhere within your network without having to worry about external security risks.
On-premises connections can also be used to connect your APIs to existing internal network resources.
This can be especially useful if you have existing infrastructure that you want to leverage for your API.
Discover more: Azure Linux Webapp Security
Verify It's Working
Now that you've set up your Azure web API, it's time to verify it's working as expected. You can test the deployed ASP.NET Core web API in Azure API Management from the Azure portal.
Open the Test tab in the Azure portal to start the testing process. Select / or the Get operation to choose the specific endpoint you want to test.
Next, click Send to initiate the test. This will send a request to the API and display the response in the test results.
If everything is working correctly, you should see the expected response from the API.
A fresh viewpoint: Azure Function Send Email
Introduction and Setup
Let's get started with Azure API Apps. We can use it to create and deploy RESTful APIs in Azure.
Azure API Apps supports RESTful APIs built with various programming languages like .NET, Java, PHP, Node.js, Python, and more.
We can configure Cross-Origin Resource Sharing (CORS) with Azure API Apps, making it a convenient choice for building APIs.
To begin, we need to create an Azure API app, which will provide a default page that we can work with.
Frequently Asked Questions
What is the difference between a web app and a web API?
A web application renders graphical content, while a web API returns non-graphical data, but the line between them can blur when considering HTML as data. This subtle distinction highlights the flexibility of web APIs to mimic web applications.
What is an Azure API?
Azure API Management is a platform that helps manage APIs across all environments, supporting the entire API lifecycle from creation to retirement. It's a cloud-based service that simplifies API management for developers and organizations.
Featured Images: pexels.com

