Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Host a RESTful API With CORS in Azure App Service

DZone's Guide to

Host a RESTful API With CORS in Azure App Service

Read on to find out how to deploy an ASP.NET Core API app to App Service with CORS support by following step by step instructions.

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

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:

  • 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 Swashbuckle.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, it's 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 appropriate 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 the 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 the case of APIs, as you can test your APIs straightaway to 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 the OK button to download the metadata associated with it. Now, consume the Service, as we do in any other client with the supported metadata.

With SnapLogic’s integration platform you can save millions of dollars, increase integrator productivity by 5X, and reduce integration time to value by 90%. Sign up for our risk-free 30-day trial!

Topics:
integration ,api ,cors ,azure app service ,restful api ,asp.net core

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}