Working With Azure API Apps


Working with Azure API APPS

 

Introduction

According to Microsoft –“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 simpler words, it is a platform for hosting the Web Apis with most common API features as out of the box service for which you don’t have to code.

We can directly host the application in a web app and leverage all the above services but there are 4 main reasons that I personally think API Apps serves better than Web App

  • 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 and Enter the name of the API and click on 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 the Azure API App some of the common Web API used packages like Newtonsoft.Json and Swasbuckle.core (Swagger) comes 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 controller but for this demo we are going to use the empty controller.

 

  • Name the controller as Calculator Controller and as we selected the WEB API 2 template so its going to be derived from the APIController. Add the following Code​​ 

 

 

public​​ class​​ CalculatorController​​ :​​ ApiController

 ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ // GET api/values/5

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ [HttpGet]

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ [System.Web.Http.Route("Api/Add")]

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ public​​ int​​ Add(int​​ a,​​ int​​ b)

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ return​​ a + b;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ [HttpGet]

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ [System.Web.Http.Route("Api/Sub")]

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ public​​ int​​ Sub(int​​ a,​​ int​​ b)

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ return​​ a - b;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ [HttpGet]

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ [System.Web.Http.Route("Api/Mul")]

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ public​​ int​​ Mul(int​​ a,​​ int​​ b)

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ return​​ a * b;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ [HttpGet]

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ [System.Web.Http.Route("Api/Div")]

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ public​​ int​​ Div(int​​ a,​​ int​​ b)

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ {

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ return​​ a / b;

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 

 ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ }

 

 

  • The above code is going to hit with the following route API/Controller/Action/ID so we need to go to WebAPI Config and update it accordingly.

 

 

WebApiConfig

 

config.Routes.MapHttpRoute(

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ name:​​ "DefaultApi",

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ routeTemplate:​​ "api/{controller}/{action}/{id}",

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ defaults:​​ new​​ { id =​​ RouteParameter.Optional }

 ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​ ​​​​ );

 

 

 

  • Now we need to publish the API to azure API App instance. Right Click on the project and click on publish.

 

 

 

 

 

 

 

  • Select the Azure API app and enter your credentials to authenticate and login to your subscription. If you have an existing API App you can directly skip to #

 

  • Enter the name and select a Resource Group, AppService plan in the approproiate subscription.

if you don’t have the Resource Group or app service plan you can create from the same wizard by clicking on the respective new button and passing the appropriate values. Once all the values are passed, Click on Create button. It’s going to create a Azure API Web App in your azure account.

 

  • Once Completed the publish metadata file will be downloaded and then you can click on the publish button to push your binaries.

 

  • Once the publish is completed, it’s going to open up the url in your browser.​​ 

 

  • Append the swagger to the URL and you can see all the Methods we have created in our code above.

 

 

  • Swagger UI also gives you to test the methods by acting as a Client. We are trying the Sub method and passed two params to it and clicked on the “TRY IT NOW” button and we it contacted the api and return the result. This is very useful in case of APIs as you can straightaway test your APIs and see if its working fine or not without writing a single line of code.

 

API Definition

 

  • Now, go to the azure portal and go to your resource and select the API Definition. You can see that it give you and option to export the metadata to the PowerApps and Microsoft Flow.

 

 

Support for creating an Azure API Client from Visual Studio

  • 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 the ​​ azure asset that we have created and click on OK button to download the metadata associated with it. Now Consume the Service like we do in any other Client with the supported metadata.

 

Summary

Azure API App is a Platform as a service solution under the umbrella of Web Apps. It gives you inbuilt features to work with APIs seamlessly like inbuilt support for metadata using swagger, along with the API definition support on the portal. It also gives us the option of the publishing the API into the azure marketplace which also can be consumed by the Logic App as a connector.

Comments

comments

Leave a Reply

Your email address will not be published.