Azure Functions are serverless applications on Microsoft Azure Cloud Platform without worrying about the infrastructure to run it. It’s very similar to the lambda function in the AWS Cloud.
The below Azure Function returns a string message as follows
public static class BasicExample
{
[FunctionName("BasicExample")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "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);
}
}
Simply press F5 to start debugging the Azure function, it will open a console which will provide a URL to access the browser.
Azure Functions Core Tools
Core Tools Version: 4.0.5198 Commit hash: N/A (64-bit)
Function Runtime Version: 4.21.1.20667
[2024-03-28T05:48:45.707Z] Found D:\Workspace\30DayChallenge.Net\AzureFunctionExample\AzureFunctionExample.csproj. Using for user secrets file configuration.
Functions:
BasicExample: [GET,POST] http://localhost:7073/api/BasicExample
Open the URL “http://localhost:7073/api/BasicExample” in the browser to start running the function endpoint.
The response returned from the browser.
This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.
Modify the URL with additional query parameters as ?name=Sukhpinder
http://localhost:7073/api/BasicExample?name=Sukhpinder
The response returned from the browser.
Hello, Sukhpinder. This HTTP triggered function executed successfully.
GitHub — ssukhpinder/30DayChallenge.Net
Thank you for being a part of the C# community! Before you leave:
Follow us: X | LinkedIn | Dev.to | Hashnode | Newsletter | Tumblr
Visit our other platforms: GitHub | Instagram | Tiktok | Quora | Daily.dev
More content at C# Programming
Also published here.