Azure Functions vs Web API

.NET Core Web API VS Azure Functions

This Azure article will discuss the key differences between Web API vs Azure functions

Azure Functions vs Web API

Many companies want to migrate their existing .Net Core web APIs to the Azure Functions. We will discuss the key differences between .Net Core Web API and Azure Functions.

If You see the syntax, the .Net Core Web API will look something like the below

[Route("Employeess/{employeeid}")]
[HttpGet]

public async Task<IActionResult> OnPostAsync(string employeeID)
{

// Code implementation HTTP Request Headers

//Code implementation Handle HTTP Request Message

//Getting the response
var MyListData = newresponse.Content.ReadAsAsync<List<RequestLeave>>();

ViewData["RequestLeave"] = MyListData.Result;

return Page();
}
}

Now, If you see the syntax for an Azure Function, It will look something like the below

public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            string responseMessage = string.IsNullOrEmpty(name)
                ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This HTTP triggered function executed successfully.";

            return new OkObjectResult(responseMessage);
        }
    }

If you see both syntaxes closely, you can see both look similar in terms of getting the HTTP request, processing the same, and providing the response. So it will be a bit easier to migrate the existing .NET Core Web API to Azure Functions with maybe minimal changes.

But if you will see, Azure Functions have a Static modifier, whereas you can write the Web APIs without static modifiers.

One more thing in the case of Web API, the controller creates an HttpContext instance internally to handle the different data like headers, query strings, cookies, sessions, etc. But in the case of Azure Functions, the HTTPRequestMessage instance only handles headers, request bodies, query strings, etc., but it does not bother for the data such as cookies or sessions like web API. It is Stateless.

Another difference, in this case, is, For Web API, we need to put the HttpGet, HttpPut, HttpPost, etc based on the operation you are performing with the help of the Web API. Whereas in the case of the Azure Functions, each function will have a definition of HTTP and the routes on the function.json file.

One more last thing is, To mention the base URI, we need to add the base URI at the controller level with the help of the Route parameter, whereas, in the case of the Azure Functions, It will be taken care of in the host.json file.

You may also like following the articles below

Final Thoughts

This article discussed Azure Function vs. Web API and the key differences between Web API and Azure Functions. Thanks for reading this article !!!