Tutorial: Create an HTTP-triggered Azure Function with C#

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.

Prerequisites

Before you begin, ensure you have the following installed:

1. Create a New Azure Functions Project

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.

2. Create an HTTP Trigger Function

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.

3. Examine the Function Code

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:

4. Run the Function Locally

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

5. Test the Function

Open your web browser or use a tool like curl to test the function:

You should receive a response like "Hello, AzureUser!" or "Hello, World!".

Deploying to Azure (Brief Overview)

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.

Next Steps

Add Cosmos DB Binding Add Queue Storage Trigger