Host a RESTful API with CORS in Azure App Service

This post explains the required steps to deploy an ASP.NET Core API app to App Service with CORS support. You can configure the app using command-line tools and deploy the app using Git.

Azure App Service provides a highly scalable, self-patching web hosting service. In addition, App Service has built-in support for Cross-Origin Resource Sharing (CORS) for RESTful APIs. 

The required steps:


Install Git.
Install .NET Core.
Create local ASP.NET Core app

API apps in Azure App Service offer features that make it easier to develop, host, and consume APIs in the cloud and on-premises. With API apps you get enterprise grade security, simple access control, hybrid connectivity, automatic SDK generation, and seamless integration with Logic Apps. In simple words, it is a platform to host the Web apps with the most common API features for which you don’t have to code.

We can directly host the Application in a Web app and leverage all the Services given above:

Inbuilt Swagger Integration.
Ability to push your API APPS into Azure MarketPlace.
API definition.
Support for creating an Azure API client from Visual Studio.
We are going to create a demo and discuss all the 4 main reasons along with the demo.

Create an API from Visual Studio and host it in Azure API app

Go to Visual Studio -> Visual C# -> Web -> ASP.NET Web Application, enter the name of the API and click OK button. Now, select Azure API app from the dialog box. We can select Web API as well and then publish it as Azure API app, which will also serve the same purpose.

Inbuilt Swagger Integration

  • As we have selected an Azure API app, some of the common Web API used packages like Newtonsoft.Json and Swasbuckle.core (Swagger) come directly in the template.
  • Create an API Controller by right clicking on the Controller and Add -> Controller.
  • Now, select the Web API 2 Empty Controller. You can use any of the controllers but for this post, we are going to use the empty controller.
  • Name the controller as Calculator Controller and as we selected the WEB API 2 template, its going to be derived from the APIController. 
  • Now, we need to publish the API to Azure API app instance. Right click on the project and click Publish.
  • Select Azure API app and enter your credentials to authenticate and login. 
  • Enter the name and select a Resource Group, AppService plan in the approproiate subscription. If you don’t have the Resource Group or an app Service plan, you can create from the same wizard by clicking the respective new button and pass the appropriate values. Once all the values are passed, click Create button. It’s going to create an Azure API Web app in your Azure account.
  • Once completed, the published metadata file will be downloaded and subsequently you can click Publish button to push your binaries.
  • Once the publish is complete, it’s going to open up the URL in your Browser.
  • Append the swagger to the URL and you can see all the Methods, which we have created in our code above.
  • Swagger UI also allows you to test the methods by acting as a Client. We are trying the Sub method and passed two params to it and clicked the “TRY IT NOW” button. Now, we will contact the API and return the result. This is very useful in case of APIs, as you can straightaway test your APIs and see if it’s working fine or not without writing a single line of code.
  • Now, go to Azure and go to your resource. Select the API definition and you can see that it gives you an option to export the metadata to the PowerApps and Microsoft Flow.
  • Now, add a new project and select a console Application.
  • Right click on the project and add REST API client.
  • Now, add the metadata URL or select an Azure asset, which we have created and click OK button to download the metadata associated with it. Now, consume the Service, as we do in any other client with the supported metadata.

Copyright © All Rights Reserved - C# Learners