
In this Azure article, we will discuss the key differences between .Net Core Web API VS Azure Functions.
Table of Contents
.NET Core Web API VS Azure Functions
Nowadays many companies want to migrate their existing .Net Core web APIs to the Azure Functions. We will discuss here the key differences between .Net Core Web API and Azure Functions.
If You will see the syntax wise, the .Net Core Web API will look something like 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 will see the syntax for an Azure Function, It will look something like 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 will see both syntaxes closely, you can able to see both look a bit similar in terms of both getting the HTTP request and processing the same and providing the response. So it will be a bit easy 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 body and 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 below articles
- How to Create And Consume Azure Function From ASP.NET Core
- Azure Functions .Net Core Version How To Create
Final Thoughts
In this article, we discussed .NET Core Web API VS Azure Functions and the key differences between .NET Core Web API and Azure Functions. Thanks for reading this article !!!