This tutorial guides you through creating a simple Azure Function that responds to HTTP requests using C#. You'll learn how to set up your development environment, write the function code, and test it locally.
Before you begin, ensure you have the following installed:
npm install -g azure-functions-core-tools@3 --unsafe-perm true
Open your terminal or command prompt and navigate to the directory where you want to create your project. Then, run the following commands:
func init MyHttpFunction --dotnet
cd MyHttpFunction
This initializes a new .NET-based Azure Functions project in a folder named MyHttpFunction
.
Now, let's add an HTTP-triggered function to our project. Run this command inside your project directory:
func new --name HttpTriggerCSharp --template "HTTP trigger" --authlevel "anonymous"
This creates a new function named HttpTriggerCSharp
using the "HTTP trigger" template with anonymous authentication.
Open the HttpTriggerCSharp.cs
file in your code editor. You'll see code similar to this:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace MyHttpFunction
{
public static class HttpTriggerCSharp
{
[FunctionName("HttpTriggerCSharp")]
public static async Task 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);
}
}
}
Explanation:
[FunctionName("HttpTriggerCSharp")]
: Defines the name of the function.[HttpTrigger(...)]
: Configures the trigger. Here, it accepts GET and POST requests, with AuthorizationLevel.Anonymous
allowing access without a key.HttpRequest req
: Represents the incoming HTTP request.ILogger log
: Used for logging messages.Save your changes and start the Azure Functions host from your project's root directory:
func start
You will see output indicating that the function host is running and the URL for your HTTP trigger. It usually looks something like this:
Http Functions:
HttpTriggerCSharp: [GET,POST] http://localhost:7071/api/HttpTriggerCSharp
Open your web browser or use a tool like curl
to test the function:
http://localhost:7071/api/HttpTriggerCSharp?name=AzureUser
curl
or a similar tool:
curl -X POST http://localhost:7071/api/HttpTriggerCSharp -d "{\"name\":\"World\"}" -H "Content-Type: application/json"
You should receive a response like "Hello, AzureUser!" or "Hello, World!".
To deploy, you'll typically use Azure CLI or Visual Studio Code's Azure Functions extension. Ensure you've logged in to Azure (`az login`) and have created an Azure Function App resource.
Using Azure CLI:
az functionapp deployment source config-zip -n -g --src-zip AzureFunctions.zip
Remember to zip your project files first.