다음을 통해 공유


Azure: Deploy Your First Azure API App

API Apps are web services (like Web Apps). We construct code that will be deployed on Azure and there are applications designed to call these API Apps.
On API Apps there is no UI (User Interface), its main responsibility is to manage data.

API Apps Benefits

When using API Apps we can take advantage of the benefits they offer, these are :

 ↑ Back to top

Create API App

From the Azure Portal click Create a resource and search for "API App" and click Create.

Back to top

Azure API App Parameters

In the image below we can see how to fill up the parameters to create API App.

* More details about App Service Plan you can find at this link.

After deployment ends, in the Resource Group container we will see the below three elements :

Back to top

The Visual Studio Story

Until now all the previous steps were easy. Many of us probably believe that now the hard job begins. But the next steps will prove the opposite.

Open the Visual Studio, go to File - New - Project, select from Visual C# - Web - **ASP.NET Web Application (.NET Framework)**and click OK. In the next screen select "Azure API App" and click OK.

The next step is to Publish the project to Azure API App service. Select Publish - Microsoft Azure App Service - Select Existing and click Publish.

Follow the steps, select the Subscription, Resource Group and click OK.

Build and Publish of the API App started as the next piece of log shows.

------ Build started: Project: cloudopszone-API_App, Configuration: Release Any CPU ------------ Publish started: Project: cloudopszone-API_App, Configuration: Release Any CPU ------...Publish Succeeded.Web App was published successfully http://cloudopszone.azurewebsites.net/========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==================== Publish: 1 succeeded, 0 failed, 0 skipped ==========

After build finishes and Publishing procedure ends, a page will open on a browser which informs us that the App Service app is up and running and offers to us a number methods like GET, POST, PUT, DELETE.

 

Back to top

More Details About API App

OK, we all understand that what already mentioned is not enough, so we will say a few more about the SwaggerConfig.cs and the WebApiConfig.cs.

WebApiConfig.cs

The WebApiConfig.cs located on the VS solution explorer inside the folder App_Start.

using System;using System.Collections.Generic;using System.Linq;using System.Web.Http;namespace cloudopszone_API_App{public static class WebApiConfig{public static void Register(HttpConfiguration config){// Web API configuration and services// Web API routesconfig.MapHttpAttributeRoutes();config.Routes.MapHttpRoute(name: "DefaultApi",routeTemplate: "api/{controller}/{id}",defaults: new { id = RouteParameter.Optional });}}}

In theWebApiConfig.cswhat is quite important is the MapHttpRoute which is responsible to route incoming HTTP requests to a method action on a Web API controller.

Below we can see the methods that our API App supports. And we must notice the namespace Swashbuckle.Swagger.Annotations which is responsible for Swagger.

using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Net.Http;using System.Web.Http;using Swashbuckle.Swagger.Annotations;namespace cloudopszone_API_App.Controllers{public class ValuesController : ApiController{// GET api/values[SwaggerOperation("GetAll")]public IEnumerable<string> Get(){return new string[] { "value1", "value2" };}// GET api/values/5[SwaggerOperation("GetById")][SwaggerResponse(HttpStatusCode.OK)][SwaggerResponse(HttpStatusCode.NotFound)]public string Get(int id){return "value";}// POST api/values[SwaggerOperation("Create")][SwaggerResponse(HttpStatusCode.Created)]public void Post([FromBody]string value){}// PUT api/values/5[SwaggerOperation("Update")][SwaggerResponse(HttpStatusCode.OK)][SwaggerResponse(HttpStatusCode.NotFound)]public void Put(int id, [FromBody]string value){}// DELETE api/values/5[SwaggerOperation("Delete")][SwaggerResponse(HttpStatusCode.OK)][SwaggerResponse(HttpStatusCode.NotFound)]public void Delete(int id){}}}

SwaggerConfig.cs

The SwaggerConfig.cs located on the VS solution explorer inside the folder App_Startand if we are observant we notice that swagger is disabled. So, we will show you how to enable and call Swagger in our API App.

To enable Swagger just uncomment the lines 190 and 194, build our solution and Publish our app to Azure.

/*}).EnableSwaggerUi(c =>{*/}).EnableSwaggerUi(c =>{

Now, we are ready to open Swagger. We just need to add /swagger

Back to top

Conclusion

Microsoft Azure has a wide range of services, one of these services is Azure API App, which gives the opportunities to developers write code for their services by supporting a wide range of program languages.

 

Back to top

See Also

GitHub

Microsoft docs